【问题标题】:Web Scraping with BeautifulSoup and Python : failed to extract text使用 BeautifulSoup 和 Python 进行网页抓取:提取文本失败
【发布时间】:2021-05-02 00:02:37
【问题描述】:

我尝试抓取 website。但我未能提取每个项目的描述。这是我的代码:

from bs4 import BeautifulSoup
import requests

url = "http://engine.ddtc.co.id/putusan-pengadilan-pajak"
response = requests.get(url)
data = response.text
soup = BeautifulSoup(data, 'html.parser')
puts =soup.find_all("div",{"class":"p3-search-item"})
for put in puts:
    title = put.find("div", {"class":"p3-title"}).text
    cat = put.find("div", {"class":"p3-category"}).text
    date = put.find("div", {"class":"search-result-item-meta"}).text
    link = put.find("a").get("href")
    put_response = requests.get(link)
    put_data = put_response.text
    put_soup = BeautifulSoup(put_data, "html.parser")
    put_description = put_soup.find("div",{"id": "modal-contents-pp"}).text
    print("Judul Putusan:", title, "\nKategori:", cat, "\nTanggal:", date, "\nLink:", link, "\nDescription:", put_description)

所以我未能提取描述。 描述只显示空白和几个单词。如果我们单击每个项目的链接,则可以显示完整的描述。 非常感谢任何帮助。

【问题讨论】:

  • 似乎链接打开了通过 javascript 加载的页面,因此您需要一个可以执行 javascript 的解决方案。在 StackOverflow 中搜索 Selenium。
  • 但我不明白,当我检查网页时,它显然包含 div 等,我看不出它是否使用 javascript

标签: python web-scraping beautifulsoup


【解决方案1】:

我认为您需要更改 put_description 字段:

from bs4 import BeautifulSoup
import requests

url = "http://engine.ddtc.co.id/putusan-pengadilan-pajak"
response = requests.get(url)
data = response.text
soup = BeautifulSoup(data, 'html.parser')
puts =soup.find_all("div",{"class":"p3-search-item"})
for put in puts:
    title = put.find("div", {"class":"p3-title"}).text
    cat = put.find("div", {"class":"p3-category"}).text
    date = put.find("div", {"class":"search-result-item-meta"}).text
    link = put.find("a").get("href")
    put_response = requests.get(link)
    put_data = put_response.text
    put_soup = BeautifulSoup(put_data, "html.parser")
    put_description = put_soup.find("div",{"class": "p3-desc"}).text
    print("Judul Putusan:", title, "\nKategori:", cat, "\nTanggal:", date, "\nLink:", link, "\nDescription:", put_description)

【讨论】:

  • 我要提取的不是p3-desc,而是打开链接后的描述
猜你喜欢
  • 2020-08-09
  • 1970-01-01
  • 2019-02-15
  • 2020-10-04
  • 2021-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-16
相关资源
最近更新 更多