我正在尝试仅提取html中的iid代码,以便可以将其附加到url并打开所需的页面。
我可以通过指定标签的类来找到所需的标签。但是我在输出中还得到了4个其他标签。我想要的只是第一个标签“ 183988596953”中的iid
我尝试使用此代码仅指定idd
rslt_table = soup.find_all("iid",{"div class": "lvpic pic img left"})
这似乎只返回一个空列表[]
当我用下面的第二行代码替换上面的代码行时,我得到的输出是我提到的带有4个标签的输出
from bs4 import BeautifulSoup
import requests
import re
urls = ['https://www.ebay.co.uk/sch/i.html?_from=R40&_trksid=m570.l1313&_nkw=goldfinger+quad']
#https://www.ebay.co.uk/sch/i.html?_from=R40&_trksid=m570.l1313&_nkw=
def find_id(urls):
for url in urls:
session = requests.session()
response = session.get(url)
#soup = BeautifulSoup(response.content, "lxml")
soup = BeautifulSoup(response.content, "html.parser")
rslt_table = soup.find("div", {"class": "lvpic pic img left"})
return(rslt_table)
我的搜索网址是https://www.ebay.co.uk/sch/i.html?_from=R40&_trksid=m570.l1313&_nkw=goldfinger+quad'
全名是
<div class="lvpic pic img left" iid="183988596953">
<div class="lvpicinner full-width picW">
<a class="img imgWr2" href="https://www.ebay.co.uk/itm/GOLDFINGER-1964-Style-A-B-UK-Cinema-High-Quality-Repro-30-x-40-quad-poster/183988596953?hash=item2ad69330d9:g:rYQAAOSwrENdbmEW">
<img alt='GOLDFINGER 1964 Style A & B - UK Cinema High Quality Repro 30"x 40" quad poster' class="img" src="https://i.ebayimg.com/thumbs/images/g/rYQAAOSwrENdbmEW/s-l225.jpg"/>
</a>
</div></div>
答案 0 :(得分:1)
attrs
返回所有属性
{'class': ['lvpic', 'pic', 'img', 'left'], 'iid': '183988596953'}
def find_id(urls):
for url in urls:
session = requests.session()
response = session.get(url)
soup = BeautifulSoup(response.content, "html.parser")
return soup.find("div", {"class": "lvpic pic img left"}).attrs['iid']
iid = find_id(urls)
print(iid)
>>> '183988596953'
iid
:def find_id(urls):
for url in urls:
session = requests.session()
response = session.get(url)
soup = BeautifulSoup(response.content, "html.parser")
div = s.find_all("div", attrs={'class': 'lvpic pic img left'})
return [iid.attrs['iid'] for iid in div]