【发布时间】:2016-03-11 15:42:05
【问题描述】:
您好,我正在从 ABC 新闻网站抓取最新消息,我正在抓取的代码如下所示:
<a href="/Politics/huckabee-draws-cheers-fundraiser-west-bank-settlement/story?id=35615831" name="lpos=widget[A_3_freeformlite_4380645_homepage]&lid=link[Headline_2]">Huckabee Draws Cheers at Fundraiser for West Bank Settlement<span class="metaH_timeDay">41 minutes ago</span></a>
但你注意到我在 a 标签内有一个 span 标签,所以当我用 BeautifulSoup 抓取它时,我会得到如下信息:
41 分钟前,Huckabee 在为约旦河西岸定居点筹款活动中获得欢呼
但它给我的时间正好在我的数据旁边,我想分开 41 分钟,所以它看起来像这样:
41 分钟前,Huckabee 在为约旦河西岸定居点筹款活动中欢呼
或者至少删除它!
我的代码如下所示:
import requests
from bs4 import BeautifulSoup
url = "http://abcnews.go.com/"
r = requests.get(url)
soup = BeautifulSoup(r.content, "lxml")
for x in range(1,10):
for link in soup.find_all("a",{"name": "lpos=widget[A_3_freeformlite_4380645_homepage]&lid=link[Headline_"+str(x)+"]"}):
print link.text
print link.find_all("",{"class": "metaH_timeDay"})[0].text
print ""
有人可以帮我吗?
【问题讨论】:
标签: python html web-scraping beautifulsoup python-requests