【发布时间】:2018-05-05 19:09:29
【问题描述】:
所以 2 天前,我试图解析两个相同类之间的数据,然后 Keyur 帮了我很多,然后他把其他问题抛在脑后.. :D
现在我想获取特定类下的链接,这是我的代码,这里是错误。
from bs4 import BeautifulSoup
import urllib.request
import datetime
headers = {} # Headers gives information about you like your operation system, your browser etc.
headers['User-Agent'] = 'Mozilla/5.0' # I defined a user agent because HLTV perceive my connection as bot.
hltv = urllib.request.Request('https://www.hltv.org/matches', headers=headers) # Basically connecting to website
session = urllib.request.urlopen(hltv)
sauce = session.read() # Getting the source of website
soup = BeautifulSoup(sauce, 'lxml')
a = 0
b = 1
# Getting the match pages' links.
for x in soup.find('span', text=datetime.date.today()).parent:
print(x.find('a'))
错误:
实际上没有任何错误,但输出如下:
None
None
None
-1
None
None
-1
然后我研究并发现如果没有任何数据可以提供,find 函数不会给你任何东西。 然后我尝试使用 find_all
代码:
print(x.find_all('a'))
输出:
AttributeError: 'NavigableString' object has no attribute 'find_all'
这是类名:
<div class="standard-headline">2018-05-01</div>
我不想在这里发布所有代码,所以这里是链接 hltv.org/matches/ 以便您可以更轻松地检查课程。
【问题讨论】:
-
import bs4 as BeautifulSoup不正确。你的代码到底是什么样的? -
您是否无意或有意忘记提及您所说的班级名称?
-
你的意思是
for a in soup.find('span', text=(datetime.date.today())).parent.find_all("a"): print(a)? -
standard-headline类名下没有链接。都是文字。至少我一个也找不到。请具体。 -
标签: python-3.x web-scraping beautifulsoup urllib