Python 靓汤不回预期效果
我用beautifulsoup从一个网站上收集信息”https://www.yugiohcardguide.com/archetype/abyss-actor.html". 卡片信息设置相对整齐。下面是我试图解析的html的图片 我正在尝试获取每行中包含一张卡的信息的所有标记 下面是我使用的代码Python 靓汤不回预期效果,python,beautifulsoup,Python,Beautifulsoup,我用beautifulsoup从一个网站上收集信息”https://www.yugiohcardguide.com/archetype/abyss-actor.html". 卡片信息设置相对整齐。下面是我试图解析的html的图片 我正在尝试获取每行中包含一张卡的信息的所有标记 下面是我使用的代码 def get_card_info_from_link(self, link): new_link=pre_url+'/'+link #link to the archtype pa
def get_card_info_from_link(self, link):
new_link=pre_url+'/'+link #link to the archtype page
html=requests.get(new_link).content
soup=bs(html,'lxml')
info_rows=soup.find('tbody').find_all('tr')
found_cards=[]
# count=0
for i in info_rows:
print('='*50)
print(i)
print('='*50)
# count+=1
这是我得到的输出的链接。
使用等号分隔符的前两个输出正是我想要的,但有一点它不再输出以前的格式,而是一个包含多个标记的项,而不是每个标记都独立
我不知道问题出在哪里。也许我只是忽略了一个我没有注意到的关键细节。也许这段代码可以帮助您:
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome('C:\chromedriver_win32\chromedriver.exe')
driver.get('https://www.yugiohcardguide.com/archetype/abyss-actor.html')
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
result = soup.find('tbody').find_all('tr')
print(result)
driver.close()
html已损坏或具有未关闭的标记
<tr class="row2" valign="top">
.....
</a>
<!-- No </td></tr> -->
<tr class="row2" valign="top">
使用Regex
fixed_html = re.sub(r'</a>\s+<tr valign="top"', '</a></td></tr><tr valign="top"', html)
或者使用tidy
fixed_html = tidy.parseString(html, show_body_only=True)
然后解析固定html
soup = BeautifulSoup(fixed_html,'lxml')
info_rows = soup.find('tbody').find_all('tr')
尝试查看
html
的内容。您在浏览器中看到的一些实际内容可能是由JavaScript生成的。此外,这绝对不是一个好消息。外部链接不会。同意。这看起来像是一个案例,您可以将其简化为一个我们可以重现的示例,并清楚地显示失败案例和预期结果。代码片段工具via可以用来显示相关的html。非常感谢,它使用正则表达式工作。
fixed_html = tidy.parseString(html, show_body_only=True)
soup = BeautifulSoup(fixed_html,'lxml')
info_rows = soup.find('tbody').find_all('tr')