利用python編寫一個簡單的爬糗事百科熱圖爬蟲

python爬蟲,爬取糗事百科並保存到文件中

以下是代碼實現部分,如果對你有幫助,留下一個贊

from urllib import request
from lxml import etree
from urllib import request
import time
def page():
    for num in range(1,6):
        time.sleep(1)
        base_url = 'https://www.qiushibaike.com/imgrank/page/{0}'.format(num)
        headers = {
            'User-Agent': ' Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
        # 構造帶有請求頭的url
        req = request.Request(base_url, headers=headers)
        response = request.urlopen(req)
        html = response.read().decode('utf-8')
        htmls = etree.HTML(html)
        make_page(htmls)
# 清洗數據/存儲
def make_page(htmls):
    from urllib import request
    picture = htmls.xpath('//div[@class="thumb"]/a/img/@src')
    time.sleep(1)
    for pic_url in picture:
        src = pic_url.split('/')[-1]
        image_url = 'https:' + pic_url
        print('圖片下載中。。。。。')
        request.urlretrieve(image_url,'D:\66\ssss' )
        print('寫入完成')
page()```

*
*
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章