**
爬虫概述
Python网络爬虫是利用Python编程语言编写的程序,通过互联网爬取特定网站的信息,并将其保存到本地计算机或数据库中。
"""
批量爬取各城市房价走势涨幅top10和跌幅top10
"""
from lxml import etree
import requests
HEADERS = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"}
INDEX = "https://bj.fangjia.com/zoushi"
def process_index(url):
"""
首页处理
@param url: url
@return: 返回首页源代码
"""
res = requests.request("GET", url=url, headers=HEADERS)
res.encoding = "utf-8"
return res.text
def process_city(html):
"""
各城市首页url获取
@param html: 源代码
@return: 返回各城市名和url
"""
parse = etree.HTML(html)
text = parse.xpath('//div[@class="tab_content"]/div')
city_name = []
city_url = []
for i in text:
city_name.extend(i.xpath("./a/text()"))
city_url.extend(i.xpath("./a/@href"))
city_info = dict(zip(city_