There is no data solution when opening Notepad
check root
It may be that the data was not crawled
1.在控制台尝试打印爬取到的数据
如果有数据显示,那就可能是另外一种情况
It may be that the file was not closed in time
在爬取后要及时关闭文件,使用fp.close()语句
Case presentation
import requests
from lxml import etree
url = 'https://cs.58.com/ershoufang/'
headers = {
'User-Agent': '这里填自己的身份标识'
}
page_text = requests.get(url=url,headers=headers).text
tree = etree.HTML(page_text)
divlist = tree.xpath('//section[@class="list"]/div')
fp = open('./58.txt','w',encoding='utf-8')
for li in divlist:
title = li.xpath('./a/div[2]/div/div/h3/text()')[0]
fp.write(title+'\n')
print(title + ' ----输入成功!!!')
fp.close()
print("ok")
How to find the ID
- Find any page in the browser, right-click the mouse and select Check
- Then click on the network and scroll down to see