【问题标题】:Multi level/ multi page web scraping in pythonpython中的多级/多页网页抓取
【发布时间】:2019-01-15 10:12:11
【问题描述】:

我是数据抓取的新手,我检查了一些关于 scrapy 和 beautifulsoup 的资源,但我正在努力解决以下问题。

起始页网址是here

我想要每个项目的价格、地毯等信息,然后点击项目链接

现在我需要收集概览、便利设施、规格等部分中的内容,然后返回上一个列表页面并对该页面上列出的所有项目重复此操作。此外,点击下一步按钮并对所有条目重复相同的操作。

请告诉我如何在 python 中为这个用例实现抓取算法。这是我尝试过的一个非常基本的级别代码:

> import pandas as pd from pandas 
> import ExcelWriter 
> import requests,re,csv from bs4 
> import BeautifulSoup
>     
> for i in range(1,5):      # Number of pages plus one 
>     
> url = "https://www.commonfloor.com/listing-search?city=Mumbai&search_intent=sale&property_location_filter%5B%5D=region_52c256ec1614d&prop_name%5B%5D=Kandivali%20West&polygon=1&page=1&page_size=30".format(i);
>     
> r = requests.get(url)    
> soup = BeautifulSoup(r.content)

【问题讨论】:

  • 请贴出你目前写的代码,具体在哪里需要帮助。

标签: python web-scraping beautifulsoup


【解决方案1】:

这不是一个好斗的问题,因此,您不应该在问题的“标签”中包含 scrapy 和 scrapy-spider。您正在使用 BeautifulSoup(我应该添加的旧版本),因此您应该阅读的文档是 BeautifulSoup documentation

按照文档进行操作,包括安装,以确保您拥有更新的 BS4 版本的 BeautifulSoupSoup。我不能确定你使用的是旧的,但新的你使用“from bs4 import BeautifulSoup”作为导入语句。您使用的旧版本只是说“import beautifulsoup”

虽然听起来很刺耳,但您应该真正知道自己在使用什么。我看到您不清楚如何使用基本的 python 字符串格式和使用 for 循环。在我看来,你可能会从给初学者的 Python 课程再一次尝试中受益。这不是贬低!只是说这只会对你有利。还有……

网址 = "https://www.commonfloor.com/listing-search?city=Mumbai&search_intent=sale&property_location_filter%5B%5D=region_52c256ec1614d&prop_name%5B%5D=Kandivali%20West&polygon=1&page=1&page_size=30".format(i);

无论如何!

通常,在首先使用 bs4 进行解析时,您会发出请求,在变量中初始化 bs4,同时声明解析器的类型......在您的情况下,它将是:

import requests
from bs4 import BeautifulSoup # NOT scrapy

# This is a for loop
for i in range(1,6):
    # Notice the '{}' inside the url string, when we use format, the argument
    # to it, i.e format(argument), is what does the formating
    url = "https://www.commonfloor.com/listing-search?city=Mumbai&search_intent=sale&property_location_filter%5B%5D=region_52c256ec1614d&prop_name%5B%5D=Kandivali%20West&polygon=1&page={}&page_size=30"
    #request is made
    req = requests.get(url.format(i))
    # Soup initialised to a variable and parsere declared. "lxml" in this case
    soup = BeautifulSoup(req.content, "lxml")
    items = soup.select(".snb-tile-info")
    # this will print the main div boxes with the info you want

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-17
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多