【python爬虫】爬取Q房网房价

from lxml import etree    #从lxml中导入etree
import requests
import csv
import time

def writecsv(item):              #定义写入函数
    with open('qfang.csv','a',encoding='utf-8') as f:     
        writer = csv.writer(f)
        try:
            writer.writerow(item)
        except:
            print('writer error!')
if __name__ =='__main__':
    header={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169Safari/537.36'}       #定义表头,可以再网页的network中查看
    start_url = 'https://xiny.fang.anjuke.com/loupan/all/p'         #观察所爬网页各个页面的规律,发现其网址是https://xiny.fang.anjuke.com/loupan/all/p+数字
    for x in range (1,7):
        url = start_url + str(x)+'/'     #注意,这里在用range时一定要将x转化为str
        html = requests.get(url,headers = header)   #转码
        time.sleep(1)     #延时1秒
        selector = etree.HTML(html.text)     #用etree创建一个筛选器
        xiaoqulist = selector.xpath('//*[@id="container"]/div[2]/div[1]/div[3]/div')  #寻找每一个房价信息xpath的规律
        for xiaoqu in xiaoqulist:
            mingcheng = xiaoqu.xpath('div/a[1]/span/text()')    #寻找相应房价信息中各个小项xpath的规律
            dizhi = xiaoqu.xpath('div/a[2]/span/text()')
            mianji = xiaoqu.xpath('div/a[3]/span[3]/text()')
            junjia = xiaoqu.xpath('a[2]/p/span/text()')
            item = [mingcheng,dizhi,mianji,junjia]			#根据上述信息创建列表
            writecsv(item)			#利用上面函数将信息写入csv
            print('正在爬取:',mingcheng)


你可能感兴趣的:(python)