【问题标题】:scrape the next pages in python using Beautifulsoup使用 Beautifulsoup 在 python 中抓取下一页
【发布时间】:2017-03-21 04:35:01
【问题描述】:

我想从每个页面中抓取链接并转到下一页并执行相同的操作。这是我从第一页抓取链接的代码:

  import requests
  from bs4 import BeautifulSoup

 page='https://www.booli.se/slutpriser/goteborg/22/?objectType=L%C3%A4genhet'

 request = requests.get(page)
 soup = BeautifulSoup(request.text,'lxml')
 links= soup.findAll('a',class_='search-list__item')

 url=[]
 prefix = "https://www.booli.se"
 for link in links:
    url.append(prefix+link["href"])

我在前三页尝试了以下操作,但没有成功。

import re
import requests
from bs4 import BeautifulSoup
url=[]
prefix = "https://www.booli.se"

with requests.Session() as session:

    for page in range(4):
        response = session.get("https://www.booli.se/slutpriser/goteborg/22/?
        objectType=L%C3%A4genhet&page=%f" % page)
        soup = BeautifulSoup(response.content, "html.parser")

       links= soup.findAll('a',class_='search-list__item')
       for link in links:
           url.append(prefix+link["href"])

【问题讨论】:

  • 发生了什么错误或什么不起作用?
  • 你只做了几次。如果你想把所有的页面都刮掉,你需要反复做,只要找到新的至少一个新的url
  • 这只是一个小规模的例子。如果我能让它在 3 页上正常工作,我可以稍后运行它以获得更大的循环。问题就在这里,我希望它移动到下一页,但它没有这样做。使用 requests.Session() 作为会话:对于范围内的页面(4): response = session.get("booli.se/slutpriser/goteborg/22? objectType=L%C3%A4genhet&page=%f" % page) soup = BeautifulSoup(response.content, "html.parser") 我的跳转到下一页的循环没有通过。
  • 所有 4 页对我来说似乎都是一样的。
  • response = session.get("https://www.booli.se/slutpriser/goteborg/22/? objectType=L%C3%A4genhet&page=%f" % page)将此行更改为response = session.get("https://www.booli.se/slutpriser/goteborg/22/? objectType=L%C3%A4genhet&page=" + str(page))

标签: web-scraping beautifulsoup


【解决方案1】:

首先,您必须创建可以在一个页面上正常工作的代码。

然后你必须把你的抓取代码放在循环中

url = "https://www.booli.se/slutpriser/goteborg/22/?objectType=L%C3%A4genhet&page=1"
while True:
    code goes here

您会注意到链接末尾有一个 page=number。 您必须通过更改 page=number 来计算在这些 url 上运行循环

i=1
url = "https://www.booli.se/slutpriser/goteborg/22/?objectType=L%C3%A4genhet&page=" + str(i)
while True:
    i = i+1
    page = requests.get(url)
    if page.status_code != 200:
        break
    url = "https://www.booli.se/slutpriser/goteborg/22/?objectType=L%C3%A4genhet&page=" + str(i)

    #Your scraping code goes here
    #
    #

我使用了 if 语句,这样循环就不会永远持续下去。它将转到最后一页。

【讨论】:

  • 无论你说什么他都已经做到了。这对他没有帮助,也不是答案。此外,您每次都使用url 添加str(i),这是不正确的。
  • 我已经使用这个策略进行抓取,效果很好。不要只是评论给一个有用的解决方案 Khairul Basar
【解决方案2】:

是的,我做到了。谢谢你。这是前两页的代码:

urls=[]
for page in range(3):
    urls.append("https://www.booli.se/slutpriser/goteborg/22/? 
    objectType=L%C3%A4genhet&page={}".format(page))

page=urls[1:]
#page

import requests
from bs4 import BeautifulSoup
inturl=[]

for page in page:
    request = requests.get(page)
    soup = BeautifulSoup(request.text,'lxml')
    links= soup.findAll('a',class_='search-list__item')
    prefix = "https://www.booli.se"
    for link in links:
         inturl.append(prefix+link["href"])

【讨论】:

    猜你喜欢
    • 2016-07-01
    • 2019-03-14
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 2019-07-18
    • 1970-01-01
    相关资源
    最近更新 更多