import csv import requests from lxml import etree if __name__ == "__main__": url = 'https://jn.lianjia.com/zufang/pg{}' header = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.57' } # 爬取源码数据 headers = ["区域", "小区名称", "户型", "面积(㎡)", "价格(元/月)"] path = "链家济南租房数据.csv" with open(path, 'w', newline='') as f: csv_write = csv.writer(f) csv_head = ["区域", "小区名称", "户型", "面积(㎡)", "价格(元/月)"] csv_write.writerow(csv_head) for num in range: new_url = url.format(num) page_num1 = requests.get(url=new_url, headers=header) page_num1.encoding = ('utf-8') page_num = page_num1.text tree = etree.HTML(page_num) # 存储的就是标签对象 div_list = tree.xpath('//*[@id="content"]/div[1]/div[1]/div') with open(path, 'a', newline='') as f: csv_write = csv.writer(f) for div in div_list: quyu = div.xpath('./div/p[2]/a[1]//text()')[0] name = div.xpath('./div/p[2]/a[3]//text()')[0] area = div.xpath('./div/p[2]/text()')[4].split()[0] type = div.xpath('./div/p[2]/text()')[6].split()[0] place = div.xpath('./div//span/em/text()')[0] data_row = [quyu, name, type, area, place] csv_write.writerow(data_row) print("完成")改成获取全部信息
时间: 2024-04-26 20:21:40 浏览: 113
irq_srm.rar_SRM_The Handle
好的,针对您的需求,可以将代码修改如下:
```
import csv
import requests
from lxml import etree
if __name__ == "__main__":
url = 'https://jn.lianjia.com/zufang/pg{}'
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.57'
}
# 爬取源码数据
headers = ["区域", "小区名称", "户型", "面积(㎡)", "价格(元/月)"]
path = "链家济南租房数据.csv"
with open(path, 'w', newline='') as f:
csv_write = csv.writer(f)
csv_head = ["区域", "小区名称", "户型", "面积(㎡)", "价格(元/月)"]
csv_write.writerow(csv_head)
# 获取总页数
first_page = requests.get(url.format(1), headers=header)
first_page.encoding = ('utf-8')
first_tree = etree.HTML(first_page.text)
total_page = int(first_tree.xpath('//*[@id="content"]/div[1]/div[2]/div/a[last()-1]/text()')[0])
# 爬取每一页的数据
with open(path, 'a', newline='') as f:
csv_write = csv.writer(f)
for num in range(1, total_page+1):
new_url = url.format(num)
page_num1 = requests.get(url=new_url, headers=header)
page_num1.encoding = ('utf-8')
page_num = page_num1.text
tree = etree.HTML(page_num)
# 存储的就是标签对象
div_list = tree.xpath('//*[@id="content"]/div[1]/div[1]/div')
for div in div_list:
quyu = div.xpath('./div/p[2]/a[1]//text()')[0]
name = div.xpath('./div/p[2]/a[3]//text()')[0]
area = div.xpath('./div/p[2]/text()')[4].split()[0]
type = div.xpath('./div/p[2]/text()')[6].split()[0]
place = div.xpath('./div//span/em/text()')[0]
data_row = [quyu, name, type, area, place]
csv_write.writerow(data_row)
print("第{}页完成".format(num))
print("所有数据爬取完成")
```
这样,代码就可以自动获取总页数,然后遍历每一页获取数据。在输出的时候,也会打印出当前爬取的页数,方便查看进度。
阅读全文