使用BeautifulSoup从属性中提取href

3
我使用这种方法。
allcity = dom.body.findAll(attrs={'id' : re.compile("\d{1,2}")})

返回一个像这样的列表:
[<a onmousedown="return c({'fm':'as','F':'77B717EA','F1':'9D73F1E4','F2':'4CA6DE6B','F3':'54E5243F','T':'1279189248','title':this.innerHTML,'url':this.href,'p1':1,'y':'B2D76EFF'})" href="http://www.ylyd.com/showurl.asp?id=6182" target="_blank"><font size="3">掳虏驴碌路驴碌脴虏煤脨脜脧垄脥酶 隆煤 脢脦脝路脦露脕卢陆脫</font></a>, 
<a href="http://cache.baidu.com/c?m=9f65cb4a8c8507ed4fece763105392230e54f728629c86027fa3c215cc791a1b1a23a4fb7935107380843e7000db120afdf14076340920a3de95c81cd2ace52f38fb5023716c914b19c46ea8dc4755d650e34d99aa0ee6cae74596b9a1d6c85523dd58716df7f49c5b7003c065e76445&amp;p=8b2a9403c0934eaf5abfc8385864&amp;user=baidu" target="_blank" class="m">掳脵露脠驴矛脮脮</a>]

我该如何提取这个 href?

http://www.ylyd.com/showurl.asp?id=6182

谢谢。:)

你的问题似乎有误。我认为 "a href="#####" /a" 应该看起来像 <a href="####"></a>,但我不确定。此外,请使用四个空格缩进来描述代码。 - Marcelo Cantos
谢谢,但是Stackoverflow不能编写HTML代码。 - Chris.Jie
你调用findAll返回了两个结果——两个锚标签。你想提取这些标签中的一个href属性。为什么是第一个而不是第二个?(你想要怎样的筛选呢?) - Oddthinking
2个回答

0

你可以使用

for a in dom.body.findAll(attrs={'id' : re.compile("\d{1,2}")}, href=True):
   a['href']

0
在这个例子中,实际上没有必要使用 regex,我们可以直接调用 <a> 标签并获取它的 ['href'] 属性,就像这样:
get_me_url = soup.a['href'] # http://www.ylyd.com/showurl.asp?id=6182
# cached URL
get_me_cached_url = soup.find('a', class_='m')['href']

您可以随时使用prettify()方法更好地查看HTML代码。

from bs4 import BeautifulSoup

string = '''
[
<a href="http://www.ylyd.com/showurl.asp?id=6182" onmousedown="return c({'fm':'as','F':'77B717EA','F1':'9D73F1E4','F2':'4CA6DE6B','F3':'54E5243F','T':'1279189248','title':this.innerHTML,'url':this.href,'p1':1,'y':'B2D76EFF'})" target="_blank">
 <font size="3">
  掳虏驴碌路驴碌脴虏煤脨脜脧垄脥酶 隆煤 脢脦脝路脦露脕卢陆脫
 </font>
</a>
,
<a class="m" href="http://cache.baidu.com/c?m=9f65cb4a8c8507ed4fece763105392230e54f728629c86027fa3c215cc791a1b1a23a4fb7935107380843e7000db120afdf14076340920a3de95c81cd2ace52f38fb5023716c914b19c46ea8dc4755d650e34d99aa0ee6cae74596b9a1d6c85523dd58716df7f49c5b7003c065e76445&amp;p=8b2a9403c0934eaf5abfc8385864&amp;user=baidu" target="_blank">
 掳脵露脠驴矛脮脮
</a>
]
'''

soup = BeautifulSoup(string, 'html.parser')
href = soup.a['href']
cache_href = soup.find('a', class_='m')['href']
print(f'{href}\n{cache_href}')

# output:
'''
http://www.ylyd.com/showurl.asp?id=6182
http://cache.baidu.com/c?m=9f65cb4a8c8507ed4fece763105392230e54f728629c86027fa3c215cc791a1b1a23a4fb7935107380843e7000db120afdf14076340920a3de95c81cd2ace52f38fb5023716c914b19c46ea8dc4755d650e34d99aa0ee6cae74596b9a1d6c85523dd58716df7f49c5b7003c065e76445&p=8b2a9403c0934eaf5abfc8385864&user=baidu
'''

或者,您可以使用SerpApi的Baidu有机结果API完成相同的操作。这是一个付费API,提供5000次免费试用。

本例中的主要区别在于,您不必自己找出如何获取某些元素,因为它已经为最终用户完成了JSON输出。

从第一页结果中获取href/cached href的代码:

from serpapi import BaiduSearch

params = {
  "api_key": "YOUR_API_KEY",
  "engine": "baidu",
  "q": "ylyd"
}

search = BaiduSearch(params)
results = search.get_dict()

for result in results['organic_results']:
  # try/expect used since sometimes there's no link/cached link
  try:
    link = result['link']
  except:
    link = None
  try:
    cached_link = result['cached_page_link']
  except:
    cached_link = None
  print(f'{link}\n{cached_link}\n')

# Part of the output:
'''
http://www.baidu.com/link?url=7VlSB5iaA1_llQKA3-0eiE8O9sXe4IoZzn0RogiBMCnJHcgoDDYxz2KimQcSDoxK
http://cache.baiducontent.com/c?m=LU3QMzVa1VhvBXthaoh17aUpq4KUpU8MCL3t1k8LqlKPUU9qqZgQInMNxAPNWQDY6pkr-tWwNiQ2O8xfItH5gtqxpmjXRj0m2vEHkxLmsCu&p=882a9646d5891ffc57efc63e57519d&newp=926a8416d9c10ef208e2977d0e4dcd231610db2151d6d5106b82c825d7331b001c3bbfb423291505d3c77e6305a54d5ceaf13673330923a3dda5c91d9fb4c57479c77a&s=c81e728d9d4c2f63&user=baidu&fm=sc&query=ylyd&qid=e42a54720006d857&p1=1
'''

免责声明,我是SerpApi的员工。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接