python 抓取某网站文本内容并保存到文件

 某网站正常情况下一次只能显示一句俏皮话,为了多多收集俏皮话,可以利用python\来抓取。

python 抓取某网站文本内容并保存到文件:

 1 import requests
 2 from lxml import html
 3 etree = html.etree
 4 
 5 txt=''
 6 count=0
 7 while True:
 8     resp=requests.get("https://www.nihao???.com/").text
 9     html=etree.HTML(resp)
10     txt1=html.xpath("//section/div/*/text()")[0]
11     txt+=txt1+"\n"
12     # print(txt1)   
13     count+=1
14     print(count)
15     if count>10:break
16 print(txt)    
17 with open("nihaowu.txt","wt") as file:
18     file.write(txt)    
19 #    print(txt)
20 print("----------game over-----------")

 

posted @ 2022-01-23 19:55  paoPaoLong_liu  阅读(244)  评论(0)    收藏  举报