Network data crawler - encountered no data 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

#这是一个爬取58同城二手房的爬虫代码
import requests
from lxml import etree
url = 'https://cs.58.com/ershoufang/'
headers = {
    
    
    'User-Agent': '这里填自己的身份标识'
}
#爬取页面源码
page_text = requests.get(url=url,headers=headers).text
# print(page_text)  #可以检测是否爬取到数据
#数据解析
tree = etree.HTML(page_text)
# print(tree)
divlist = tree.xpath('//section[@class="list"]/div')
# print(divlist)
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

  1. Find any page in the browser, right-click the mouse and select Check
  2. Then click on the network and scroll down to see
    insert image description here

Guess you like

Origin blog.csdn.net/qq_46304554/article/details/128511108