初学python遇到的问题
import requestsfrom lxml import etree
import csv
import time
def spider():
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'}
pre_url = 'https://shenzhen.qfang.com/sale/f1'
for x in range(1,11):
html = requests.get(pre_url+str(x), headers=headers)
time.sleep(2)
selector = etree.HTML(html.text)
house_list = selector.xpath('//*[@id="cycleListings"]/ul/li')
for house in house_list:
name = house.xpath('div/div/a/text()')
house_layout = house.xpath('div/div/p/text()')
item =
data_writer(item)
print('正在爬取', xiaoqu)
def data_writer(item):
with open('qfang.csv', 'a', encoding='utf-8', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(item)
if__name__=='__main__':
spider()
我错哪儿了,哪儿错了,:'(weeqw:'(weeqw:'(weeqw 解决了,if'__name__'网上的例子漏了引号 建议你用vscode,或者用个sublimetext。这样也方便看出 强烈推荐PyCharm软件 用记事本的都是大佬 建议把条件改成正则表达式 初学还是用pycharm比较好,有错误会有提示,方便找到问题。 用个好点的编辑器吧,vscode 或者pycharm pycharm。。。
页:
[1]
2