BeautifulSoup在marquee中找到href



我使用bs4从滚动字幕中抓取链接。我可以获得marquee数据,它作为bs4-resultSet元素返回。但是,我似乎无法访问数据中的href。我确信我错过了一些东西,因为我是网络抓取的新手,并感谢任何人的指导。

注意:我可以用硒和铬驱动程序轻松地获得链接,但这需要很长时间。


返回所有字幕数据:

url = 'https://drugs.globalincidentmap.com/'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
marquee = soup.select('div', class_='h-48') 
print(marquee)

但是,当我试图进一步深入数据时,我会得到空列表或NoneType/KeyErrorAttributeError

for a in marquee.find_all('a', href=True):
link = a.find('div', class_=':nth-child')

for a in marquee.find_all('a', href=True):
link = a.find('div', class_='flex p-2')

字幕中的链接

我可以通过硒和铬驱动程序轻松获得链接

可能是因为带有h-48类的div是用JavaScript加载的;即使不是,我也不认为soup.find('div', class_='h-48')会起作用,因为该元素有更多的类,并且您需要将所有作为class_[并且我不认为soup.select('div', class_='h-48')会给出您期望的确切结果-select实际上不应该有class_参数-只是一个CSS选择器字符串]。

soup.find('div', attrs={'class':'h-48'})soup.select('div.h-48')可以在JS加载后形成的html上工作,但您需要硒来获得



幸运的是,我认为您想要的数据已经在提取的html中,只是格式不同-您可以使用提取字典列表(mqCont)

# import json
marq = soup.find('marquee', attrs={'class':'h-48'})
if marq is None: print('Could Not Find marquee.h-48')
if not marq.get(':contents'): print('marquee.h-48 has no [:contents] attr')
try: mqCont =  json.loads(marq.get(':contents', '[]'))
except Exception as e:
mqCont = []
print('failed to parse marquee.h-48[:contents] <---', e)

或者,很快(如果你确信调试/崩溃不会有任何错误):

mqCont = json.loads(soup.select_one('marquee.h-48').get(':contents', '[]'))

您可以使用[m['url'] for m in mqCont if 'url' in m]获取新闻文章的链接列表,但由于您试图使用class_='flex p-2'获取find,因此可能需要.../event_detail?id=...链接。你可以将它们形成如下

evtUrls = [f"{url.strip('/')}/event_detail?id={m['id']}" for m in mqCont if 'id' in m]

您还可以通过执行以下操作将字典列表作为一个表(带有pandas)查看:

# import pandas
omitKeys = ['domain_event_types', 'country']
for i, m in enumerate(mqCont):
mDesc = ' '.join(w for w in BeautifulSoup(
m['description'] if 'description' in m else ''
).get_text().split() if w)
if mDesc: m['description'] = mDesc
if 'id' in m: m['eventUrl'] = f"{url.strip('/')}/event_detail?id={m['id']}"
mqCont[i] = {k:v for k, v in m.items() if k not in omitKeys}
mqcDF = pandas.DataFrame(mqCont).dropna(axis='columns', how='all').set_index('id')

以及CCD_ 18的前5行[总共100行]:

宾夕法尼亚州,美国知><14>>td style="ext-align:right;">8<2022-12-022T18:44:43.000000Z>https://drugs.globalincidentmap.com/event_detail?id=11919404知022-12-022T18:36:37.000000 Z>td style="text align=left;">https://drugs.globalincidentmap.com/event_detail?id=11919401<14>>td style="text-align:right;">1<2022-12-022T18:29:25.000000Z>>td style="text align=left;">https://drugs.globalincidentmap.com/event_detail?id=11919394
idurlupdated_ateventUrl
2022-12-01 18:36:53严重宾夕法尼亚州-照片-嫌疑人-多县毒品案证据https://www.wfmz.com/news/area/berks/photos-suspects-evidence-in-multi-county-drug-bust/collection_bf795c98-71ad-11ed-99fe-4305f426699b.html#1[69新闻]宾夕法尼亚州-照片:嫌疑人,多县缉毒案证据"当局表示,他们缴获的证据包括27.5公斤可卡因和5.5公斤芬太尼,前者的潜在街头价值为270万美元"阅读全文:https://www.wfmz.com/news/area/berks/photos-suspects-evidence-in-multi-county-drug-bust/collection_bf795c98-71ad-11ed-99fe-4305f426699b.html#141.2033-77.1945
1119194012022-12-01 18:33:01严重[terracestandard.com]加拿大-在公元前4年,与地狱天使系领带的男子面临"严重指控"时缴获毒品和枪支;CFSEU表示,缴获的毒品包括7.75公斤可卡因、4公斤大麻、1.9公斤甲基苯丙胺、248粒羟考酮药丸等"阅读全文:https://www.terracestandard.com/news/alleged-drug-traffickers-on-vancouver-island-with-hells-angels-ties-face-serious-charges/49.6506-125.4491452022-12-02T18:36:307.000000 Z
111919397https://avas.mv/en/125385[avas.mv]马尔代夫-海关查获的价值200万MVR的毒品"马尔代夫海关查获1.34公斤通过快递走私到马尔代夫的毒品"阅读全文:https://avas.mv/en/1253854.1755https://drugs.globalincidentmap.com/event_detail?id=11919397
111919394231100 South Willow Avenue,Compton,CA,美国2022-11-29 18:23:50[foxla]加利福尼亚州-在康普顿缴获价值400万美元的非法毒品"当局表示,根据对该住宅的搜查令,缴获了约5.5磅疑似焦油海洛因、10公斤疑似粉末可卡因、6公斤疑似粉末芬太尼、6000粒疑似含有芬太尼的摇头丸和25.4万粒疑似芬太尼药丸,总市值估计为417万美元"阅读全文:https://www.foxla.com/news/4-million-worth-of-illegal-drugs-seized-in-compton33.896-118.218
111919392166巴基斯坦瓜达尔https://pakobserver.net/convoy-of-camels-loaded-with-drugs-seized/[pakobserver.net]巴基斯坦-满载缴获毒品的骆驼车队"在搜查骆驼携带的货物时,ANF官员发现里面装满了毒品(大麻)。这些毒品重约1.4吨"阅读全文:https://pakobserver.net/convoy-of-camels-loaded-with-drugs-seized/25.1313https://drugs.globalincidentmap.com/event_detail?id=11919392

最新更新