初学,代码有点烂,有些错误先不处理。
#!/usr/bin/python
# -*- coding: <encoding name> -*-
import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws.append(['序号','楼盘名称', '面积', '价格','地址'])
# wb.save("e:sample.xlsx")
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
}
# 定义楼盘的各项信息列表,便于进行数据追加保存
name_list = []
price_list = []
area_list = []
add_list = []
i = 1
pagenum = input('请输入要抓取<楼盘网>多少页的数据:')
# 通过循环,设定pagenum数值来抓取多少页的楼盘数据
while i <= int(pagenum):
url = 'http://fs.loupan.com/xinfang/p'+str(i)
res = requests.get(url,headers = headers)
print('第>>>>> '+ str(i) + ' <<<<<页数据抓取完毕!!