[英]BeautifulSoup can't find all tags
我的目标是从我要抓取的链接中获取特定标签的数量。 我已经手动检查了标签的数量,但是我的代码找不到所有标签。
我已经尝试过不同的解析器,例如“ html.parser”,“ html5lib”和“ lxml”,但是每次都会发生错误。
我的代码:
from bs4 import BeautifulSoup
from selenium import webdriver
urls = ["http://www.basket.fi/sarjat/ottelu/?game_id=3502579&season_id=93783&league_id=4#mbt:2-400$t&0=1",
"http://www.basket.fi/sarjat/ottelu/?game_id=3502523&season_id=93783&league_id=4#mbt:2-400$t&0=1",
"http://www.basket.fi/sarjat/ottelu/?game_id=3502491&season_id=93783&league_id=4#mbt:2-400$t&0=1",
"http://www.basket.fi/sarjat/ottelu/?game_id=3502451&season_id=93783&league_id=4#mbt:2-400$t&0=1",
"http://www.basket.fi/sarjat/ottelu/?game_id=3502395&season_id=93783&league_id=4#mbt:2-400$t&0=1",
"http://www.basket.fi/sarjat/ottelu/?game_id=3502407&season_id=93783&league_id=4#mbt:2-400$t&0=1"]
for url in urls:
browser = webdriver.PhantomJS()
browser.get(url)
table = BeautifulSoup(browser.page_source, 'lxml')
print(len(table.find_all("tr", {"class":["row1","row2"]})))
输出:
88
87
86
66
86
59
目标输出:
88
86
87
87
86
83
我基本上只是在您的代码中添加了延迟线。 这有助于程序等待直到网页完全加载并准备使用BS4进行解析。
另请注意,我的输出与您的目标输出不同。 但是我仔细检查了每个URL上包含“ row1”和“ row2”的“ tr”的数量,看来我的输出是准确的(也许发布问题后网站上的结果有所改变)。
码:
import time
from bs4 import BeautifulSoup
from selenium import webdriver
urls = ["http://www.basket.fi/sarjat/ottelu/?game_id=3502579&season_id=93783&league_id=4#mbt:2-400$t&0=1",
"http://www.basket.fi/sarjat/ottelu/?game_id=3502523&season_id=93783&league_id=4#mbt:2-400$t&0=1",
"http://www.basket.fi/sarjat/ottelu/?game_id=3502491&season_id=93783&league_id=4#mbt:2-400$t&0=1",
"http://www.basket.fi/sarjat/ottelu/?game_id=3502451&season_id=93783&league_id=4#mbt:2-400$t&0=1",
"http://www.basket.fi/sarjat/ottelu/?game_id=3502395&season_id=93783&league_id=4#mbt:2-400$t&0=1",
"http://www.basket.fi/sarjat/ottelu/?game_id=3502407&season_id=93783&league_id=4#mbt:2-400$t&0=1"]
for url in urls:
driver = webdriver.Chrome()
driver.get(url)
time.sleep(10)
table = BeautifulSoup(driver.page_source, 'lxml')
print(len(table.find_all("tr", {"class":["row1","row2"]})))
输出:
88
87
86
87
86
83
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.