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('\n'.join([pic,title,total_pro,back_price,price,people]))
        f.write('\n'+'#'*50+'\n')

 

posted @ 2019-03-13 16:03  晨光曦微  阅读(631)  评论(0编辑  收藏  举报