python爬虫---污言污语网站数据采集

代码:

import requests
from lxml import etree

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62"
}


def get_text():
    count = 0
    while True:
        with open("nihaowua.txt", "a") as file:
            resp = requests.get("https://www.nihaowua.com/", headers=headers, timeout=10).text
            html = etree.HTML(resp)
            content = html.xpath("//section/div/*/text()")[0]
            file.write(content + "\n")
            count += 1


get_text()

 

posted @ 2021-12-23 15:15  睡觉不困  阅读(154403)  评论(0)    收藏  举报