python - 在python中进行网络抓取时删除具有相同类引用的图像源?
问题描述
我正在尝试编写一些代码来从 transfermarkt 中提取一些数据(我正在使用的页面的链接在这里)。我一直在尝试打印俱乐部。我发现我需要访问 h2 然后访问 a 类才能获取文本。HTML代码如下
<div class="table-header" id="to-349"><a class="vereinprofil_tooltip" id="349" href="/fc-barnsley/transfers/verein/349/saison_id/2018"><img src="https://tmssl.akamaized.net/images/wappen/small/349.png?lm=1574162298" title=" " alt="Barnsley FC" class="" /></a><h2><a class="vereinprofil_tooltip" id="349" href="/fc-barnsley/transfers/verein/349/saison_id/2018">Barnsley FC</a></h2></div>
所以你可以看看我是否只是尝试 find_all("a", "class": "vereinprofil_tooltip"}) 它不能正常工作,因为它还返回没有纯文本的图像文件?但是,如果我可以先搜索 h2 然后在返回的 h2 中搜索 find_all("a", "class": "vereinprofil_tooltip"}) ,它将得到我想要的。我的代码如下。
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}
page = "https://www.transfermarkt.co.uk/league-one/transfers/wettbewerb/GB3/plus/?saison_id=2018&s_w=&leihe=1&intern=0&intern=1"
pageTree = requests.get(page, headers=headers)
pageSoup = BeautifulSoup(pageTree.content, 'html.parser')
#Players = pageSoup.find_all("a", {"class": "spielprofil_tooltip"})
Clubs = pageSoup.find_all("h2")
Club = Clubs.find("a", {"class": "vereinprofil_tooltip"})
print(Club)
我在getattr raise AttributeError(AttributeError: ResultSet object has no attribute 'find_all' 中得到错误。您可能将元素列表视为单个元素。当您打算调用 find() 时是否调用了 find_all()?
我知道这个错误意味着什么,但我一直在兜圈子,试图找到一种方法来真正正确地做到这一点并得到我想要的。任何帮助表示赞赏。
解决方案
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}
page = "https://www.transfermarkt.co.uk/league-one/transfers/wettbewerb/GB3/
plus/?saison_id=2018&s_w=&leihe=1&intern=0&intern=1"
pageTree = requests.get(page, headers=headers)
pageSoup = BeautifulSoup(pageTree.content, 'html.parser')
#Players = pageSoup.find_all("a", {"class": "spielprofil_tooltip"})
Clubs = pageSoup.find_all("h2")
print(type(Clubs)) # this can be removed, but I left it to expose how I figured this out
for club in Clubs:
print(club.text)
基本上: Clubs
是一个列表(从技术上讲,是一个 ResultSet,但行为非常相似),您需要对其进行迭代。.text
只给出文本,也可以检索其他属性。
输出如下所示:
Transfer record 18/19
Barnsley FC
Burton Albion
Sunderland AFC
Shrewsbury Town
Scunthorpe United
Charlton Athletic
Plymouth Argyle
Portsmouth FC
Peterborough United
Southend United
Bradford City
Blackpool FC
Bristol Rovers
Fleetwood Town
Doncaster Rovers
Oxford United
Gillingham FC
AFC Wimbledon
Walsall FC
Rochdale AFC
Accrington Stanley
Luton Town
Wycombe Wanderers
Coventry City
Transfer record 18/19
.text
但是,您可能也应该处理一堆空行(即,是'')。
推荐阅读
- azure-web-app-service - 应用服务堆栈设置的目的是什么?
- cron - Azure Function App - 在两个特定日期触发计时器
- c++ - Switch 在使用字符串和枚举映射时执行第一种情况而不是默认情况
- c++ - 在 SLOT 的参数中传递一个小部件
- c# - 如何解决参数 2:无法从“方法组”转换为“ResumeAfter”
' 在 c#.net 代码中进行身份验证时 - javascript - 如何将 POST 请求主体映射到各个字段(NODE.JS)
- drupal - Drupal SELECT 查询,SQL 语法中的意外错误
- javascript - amcharts_Child 树在单击之前不应打开
- neural-network - 神经网络在 XOR 门测试中不起作用
- c - 如何从文件中读取数字