我在用BeautifulSoup..。
当我运行这段代码时:
inside_branding_info = container.div.find("div", "item-branding")
print(inside_branding_info)它返回:
div class="item-branding">
<a class="item-rating" href="https://www.newegg.com/gigabyte-geforce-rtx-2060-super-gv-n206swf2oc-8gd/p/N82E16814932174?cm_sp=SearchSuccess-_-INFOCARD-_-graphics+cards-_-14-932-174-_-1&Description=graphics+cards&IsFeedbackTab=true#scrollFullInfo"><i class="rating rating-4"></i><span class="item-rating-num">(12)</span></a>
</div>但是,在HTML检查中,我看到的是:原始站点HTML
每次我运行:inside_branding_info.a.img["title"] ...python认为我想要"a“标签-项目评级”...not“href标签嵌套在div”项目-品牌“内。
如何进入"a href“标记,然后进入"img",以最终提取"title" (title = "MSI")?我要标题/品牌的项目在网站上。我是Python新手。在此之前,我只使用了R和SQL,任何帮助都将不胜感激。
发布于 2020-05-15 23:17:43
你需要选择路径。根据你提供的img ..。
soup = BeautifulSoup(data)
img = soup.select('.item-brand > img')
print(img['title'])以上这些对你来说应该是可行的。
发布于 2020-05-16 00:20:14
尝试以下几点
from bs4 import BeautifulSoup
html = """<div class="item-branding">
<a href="https://www.newegg.com/" class="item-brand">
<img src="https://www.newegg.com/" title="MSI" alt="MSI"> ==$0
</a></div>"""
soup = BeautifulSoup(html, features="lxml")
element = soup.select('.item-brand > img:nth-of-type(1)')[0]['title']
print(element)https://stackoverflow.com/questions/61829742
复制相似问题