python中的多级/多页web抓取

python中的多级/多页web抓取,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我对数据刮取是新手,我在scrapy和beautifulsoup上查看的资源很少,但我正在努力解决以下问题 起始页URL为 我想要的信息,如价格,地毯是为每个项目,然后点击项目链接 现在,我需要收集各部分的内容-概述、便利设施、规格等,然后返回到上一个列表页面,并对该页面上列出的所有项目重复此步骤。此外,点击下一步按钮并对所有条目重复相同的操作 请让我知道如何在python中为这个用例实现一个刮取算法。下面是我尝试过的一个非常基本的代码: > import pandas as pd fro

我对数据刮取是新手,我在scrapy和beautifulsoup上查看的资源很少,但我正在努力解决以下问题

起始页URL为

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

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

请让我知道如何在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)

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

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

听起来很刺耳,你应该真正知道你在用什么。我发现您不清楚如何使用基本的python字符串格式和for循环。在我看来,您可能会从再次尝试python初学者课程中获益。那可不是一种侮辱!只是说这只会对你有利。还有

网址= “”格式(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

请将您迄今为止所编写的代码以及您需要帮助的具体位置张贴出来。