【问题标题】:How to scrape all the pages in the site Python [duplicate]如何抓取网站Python中的所有页面[重复]
【发布时间】:2020-04-25 10:10:56
【问题描述】:

我正在尝试使用具有多个页面的 python 抓取一个论坛。

示例:https://www.f150forum.com/f118/would-you-buy-f150-again-463954/

如何去网站的多个页面提取每个用户的cmets?提前谢谢你

url = "https://www.f150forum.com/f118/would-you-buy-f150-again-463954/"
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.text, 'html.parser')
domains = soup.find_all("div")
posts = soup.find(id = "posts")
comments_class = soup.findAll('div',attrs={"class":"ism-true"})   
comments = [row.get_text() for row in comments_class]

【问题讨论】:

标签: python web-scraping beautifulsoup


【解决方案1】:
import requests
from bs4 import BeautifulSoup
import re

data = []
with requests.Session() as req:
    for item in range(1, 11):
        print(f"Extracting Page# {item}")
        r = req.get(
            f"https://www.f150forum.com/f118/would-you-buy-f150-again-463954/index{item}/")
        soup = BeautifulSoup(r.text, 'html.parser')
        result = [item.get_text(strip=True, separator=" ") for item in soup.findAll(
            "div", id=re.compile("^post_message_"))]
        data.append(result)

print(data)

【讨论】:

  • 谢谢,效果很好。但是我很困惑为什么我们必须使用“print('*' * 30)”这个作为打印?
  • 随意删除
  • 谢谢。我试图提取的结果是一个单一的文本。有没有办法可以将每条评论提取为列表项?
猜你喜欢
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多