【问题标题】:beautifulsoup scraping a href titlesBeautifulsoup 抓取一个 href 标题
【发布时间】:2018-06-06 16:39:20
【问题描述】:

我正在寻找使用 BeautifulSoup 刮掉 a-href 中的标题。 实际上,我的代码运行不正常。

import requests
from bs4 import BeautifulSoup
name = 'Flow'
namec = 'Flow'
url = 'http://warframe.wikia.com/wiki/' + name
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
main_text = soup.find('div', class_='mw-content-ltr mw-content-text')
ada = main_text.find_all('a', title=True)

页面是http://warframe.wikia.com/wiki/Flow

我需要像这样从 div 类 pi-data-value pi-font 中提取数据:

Missions:
Survival (DS3, T3)
Excavation (T3)
Enemies:
Arid Butcher 0.03%
Bailiff 0.03%
Bailiff Defector 0.03%
Butcher 0.03%
Drahk Master 0.03%
Drekar Manic 0.03%
Frontier Bailiff 0.03%
Frontier Butcher 0.03%
Grineer Manic 0.22%
Hyekka Master 0.03%
Infested Chroma 0.6%
Infested Mesa 0.6%
Kuva Butcher 0.03%
Kuva Drahk Master 0.03%
Kuva Hyekka Master 0.03%
Tenno Specter 0.6%
Tusk Butcher 0.03%
Other:
Orokin Tower Containers

【问题讨论】:

  • 试试main_text = soup.find('div', {'class': 'mw-content-ltr mw-content-text'})

标签: python beautifulsoup


【解决方案1】:

这不是您正在寻找的完整解决方案,但它可以帮助您入门

import requests
from bs4 import BeautifulSoup

r = requests.get("http://warframe.wikia.com/wiki/Flow")
content = BeautifulSoup(r.text,'lxml')
main_div = content.find_all('div', {'class': ["pi-data-value", "pi-font"]})[3]
child_tags = main_div.findChildren()
for child in child_tags:
      print(child.text)

根据您的需要格式化文本

【讨论】:

  • 谢谢! [3] 在您的代码中代表什么?无论如何,它完美无缺!
  • 类名有多个div标签,你要的是第3个。
【解决方案2】:

给你:

import requests
from bs4 import BeautifulSoup
name = 'Flow'
namec = 'Flow'
url = 'http://warframe.wikia.com/wiki/' + name
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
main_text = soup.find('div', id='mw-content-text').find_all('div', class_='pi-data-value pi-font')
for d in main_text:
    if d.find_next(string=True) == 'Missions:':
        snippets = str(d).split("<br/>")
        for snippet in snippets:
            snippet_soup = BeautifulSoup(snippet, 'html.parser')
            print(snippet_soup.text)

【讨论】:

  • 谢谢!它没有问题。如果我想废弃 wikia 中的另一个页面,我应该改变什么?因为它不适用于那里的所有东西
猜你喜欢
  • 1970-01-01
  • 2022-10-22
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多