【发布时间】:2022-11-30 05:58:43
【问题描述】:
from bs4 import BeautifulSoup
import requests
from csv import writer
def housing_d(page_no):
url = f"https://www.pararius.com/apartments/amsterdam/page-{page}"
web = requests.get(url) ## requesting url
html_code = web.content ## gives page contents
soup = BeautifulSoup(web.content, 'html.parser')
# print(soup.prettify) ## prettify will give html code in indent and proper way
web_page = soup.find_all('ul', class_="search-list")
with open('housing.csv', 'w', encoding='utf8', newline='') as f:
thewriter = writer(f)
header = ['Title', 'Location', 'Price', 'Area']
thewriter.writerow(header)
for division in web_page:
lists = division.find_all('li', class_="search-list__item search-list__item--listing")
for list in lists:
title = list.find('a', class_="listing-search-item__link--title").text.strip().replace('\n', '')
location = list.find('div', class_="listing-search-item__location").text.strip().replace('\n', '')
price = list.find('div', class_="listing-search-item__price").text.strip().replace('\n', '')
area = list.find('li', class_="illustrated-features__item illustrated-features__item--surface-area").text.strip().replace('\n', '')
info = [title, location, price, area]
thewriter.writerow(info)
return
for page in range(1,10):
housing_d(page)
【问题讨论】:
-
我相信您每次调用函数
housing_d时都在重新编写文件,您可能想编写一次文件,然后附加到它。重写发生在with open('housing.csv', 'w' ...另外,page应该是page_no。
标签: python html beautifulsoup export-to-csv