【发布时间】:2020-04-12 08:38:17
【问题描述】:
我想用 python 和BeautifulSoup 抓取一个网站,但我找不到页码,我只能抓取网站的第一页,我认为这个网站使用了 Ajax,当我更改页面时 URL地址不变。
这是网站的链接:
https://ihome.ir/sell-residential-apartment/th-tehran
这是我的代码,我想抓取这个网站的 20 页,抓取房屋的详细信息,如价格、地基等
import requests
from bs4 import BeautifulSoup
response = requests.get("https://ihome.ir/sell-residential-apartment/th-tehran")
soup = BeautifulSoup(response.json(), "html.parser")
prices = soup.select('.sell-value')
titles = soup.select('.title')
homes_prices = []
for home in prices:
homes_prices.append(int(''.join(filter(str.isdigit, home.getText()))))
homes_titles = []
for title in titles:
homes_titles.append(title.getText())
res = dict(zip(homes_titles, homes_prices))
for key, value in res.items():
p = str(res[key])
if len(str(res[key])) <= 2:
p += '000000000'
if len(str(res[key])) > 2:
p += '000000'
print(key.strip(), int(p))
【问题讨论】:
-
这里是check
-
@αԋɱҽԃαмєяιcαη 谢谢,如何通过此链接使用请求?
-
将此链接与
reuqests一起使用,就像任何其他链接r = requests.get(link)一样。这个页面似乎不需要任何特殊的标题。不同之处仅在于您可以获得结果r.json()而不是r.text,并且您不必使用BeautifulSoup。 -
@furas tnx 为您解答,但我想用漂亮的汤刮掉网站,我编辑我的问题。我该怎么做?
-
如果您可以直接以 JSON 格式获取它,那么不要浪费时间在 BeautifulSoup 上。
标签: python ajax web-scraping beautifulsoup python-requests