爬虫遇到了问题:name 'headers' is not defined,请问如何解决?
from lxml import etree
import requests
import csv
import time
def spider():
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0'
}
pre_url='https://hefei.qfang.com/rent/f'
for x in range(1,13):
html=requests.get(pre_url+str(x),headers=headers)
time.sleep(2)#在每一次GET后,等待2秒
selector=etree.HTML(html.text)
#先获取房源列表
house_list=selector.xpath("//*[@id='cycleListings']/ul/li")
for house in house_list:
xiaoqu=house.xpath("div[2]/div[3]/div/a/text()")[0]
huxing=house.xpath("div[2]/div[2]/p[1]/text()")[0]
area=house.xpath("div[2]/div[2]/p[2]/text()")[0]
month_price=house.xpath("div[3]/p/span[1]/text()")[0]
people=house.xpath("div/div[2]/div[4]/div[1]/p/a/text()")[0]
people_picture=house.xpath("/div/div[2]/div[4]/p/a/img/text()")[0]
item=[xiaoqu,huxing,area,month_price,people,people_picture]
data_writer(item)
print('正在抓取',xiaoqu)
def data_writer(item):
with open()as csvfile:
writer=csv.writer(csvfile)
writer.writerow(item)
if __name__ == '__main__':
spider()