51ll网产品信息保存为txt文件

import requests
from pyquery import PyQuery as pq

url='http://www.51xxx.com/Try/index/p/3'
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
    }
html=requests.get(url,headers=headers).text
doc=pq(html)
items=doc('.quan-body ul li').items()

for item in items:
    pic=item.find('img').attr('src')
    title=item.find('.quan-t a').text()
    total_pro=item.find('.quan-yh i').text()
    back_price=item.find('.quan-qh-l em').text()
    price=item.find('.quan-qh-r s').text()
    people=item.find('.quan-yj em:nth-child(2)').text()
    with open('51pro.txt','a',encoding='utf-8') as f:
        f.write('
'.join([pic,title,total_pro,back_price,price,people]))
        f.write('
'+'#'*50+'
')
原文地址:https://www.cnblogs.com/chenxi188/p/10523783.html