Python 网页抓取问题
Python Web Scraping Issue
基本上我有一个很大的 html 文档需要抓取。类似文档的一个非常简化的例子如下:
<a name = 'ID_0'></a>
<span class='c2'>Date</span>
<span class='c2'>December 12,2005</span>
<span class='c2'>Source</span>
<span class='c2'>NY Times</span>
<span class='c2'>Author</span>
<span class='c2'>John</span>
<a name = 'ID_1'></a>
<span class='c2'>Date</span>
<span class='c2'>January 21,2008</span>
<span class='c2'>Source</span>
<span class='c2'>LA Times</span>
<a name = 'ID_2'></a>
<span class='c2'>Source</span>
<span class='c2'>Wall Street Journal</span>
<span class='c2'>Author</span>
<span class='c2'>Jane</span>
该文档有大约 3500 个 'a' 标签,起初我以为每个标签都有相同的布局。所以,我写了一些类似的东西:
a_list = soup.find_all('a')
data2D = []
for i in range(0,len(a_list)):
data=[]
data.append(a_list[i]['name'])
data.append(a_list[i].find_next(text='Date').find_next().text)
data.append(a_list[i].find_next(text='Source').find_next().text)
data.append(a_list[i].find_next(text='Author').find_next().text)
data2D.append(data)
但是,由于某些 ID 缺少作者或日期,因此爬虫会采用下一个可用的作者或日期,这将来自下一个 ID。 ID_1 会有 ID_2 位作者。 ID_2 会有 ID_3 日期。我的第一个想法是以某种方式跟踪每个标签的索引,如果索引超过下一个 'a' 标签索引,则附加 null。有更好的解决方案吗?
而不是 find_next()
,我会使用 .find_next_siblings()
(or .find_all_next()
) 并获取所有标签,直到下一个 a
link 或文档结尾。这些方面的内容:
links = soup.find_all('a', {"name": True})
data = []
columns = set(['Date', 'Source', 'Author'])
for link in links:
item = [link["name"]]
for elm in link.find_next_siblings():
if elm.name == "a":
break # hit the next "a" element - break
if elm.text in columns:
item.append(elm.find_next().text)
data.append(item)
基本上我有一个很大的 html 文档需要抓取。类似文档的一个非常简化的例子如下:
<a name = 'ID_0'></a>
<span class='c2'>Date</span>
<span class='c2'>December 12,2005</span>
<span class='c2'>Source</span>
<span class='c2'>NY Times</span>
<span class='c2'>Author</span>
<span class='c2'>John</span>
<a name = 'ID_1'></a>
<span class='c2'>Date</span>
<span class='c2'>January 21,2008</span>
<span class='c2'>Source</span>
<span class='c2'>LA Times</span>
<a name = 'ID_2'></a>
<span class='c2'>Source</span>
<span class='c2'>Wall Street Journal</span>
<span class='c2'>Author</span>
<span class='c2'>Jane</span>
该文档有大约 3500 个 'a' 标签,起初我以为每个标签都有相同的布局。所以,我写了一些类似的东西:
a_list = soup.find_all('a')
data2D = []
for i in range(0,len(a_list)):
data=[]
data.append(a_list[i]['name'])
data.append(a_list[i].find_next(text='Date').find_next().text)
data.append(a_list[i].find_next(text='Source').find_next().text)
data.append(a_list[i].find_next(text='Author').find_next().text)
data2D.append(data)
但是,由于某些 ID 缺少作者或日期,因此爬虫会采用下一个可用的作者或日期,这将来自下一个 ID。 ID_1 会有 ID_2 位作者。 ID_2 会有 ID_3 日期。我的第一个想法是以某种方式跟踪每个标签的索引,如果索引超过下一个 'a' 标签索引,则附加 null。有更好的解决方案吗?
而不是 find_next()
,我会使用 .find_next_siblings()
(or .find_all_next()
) 并获取所有标签,直到下一个 a
link 或文档结尾。这些方面的内容:
links = soup.find_all('a', {"name": True})
data = []
columns = set(['Date', 'Source', 'Author'])
for link in links:
item = [link["name"]]
for elm in link.find_next_siblings():
if elm.name == "a":
break # hit the next "a" element - break
if elm.text in columns:
item.append(elm.find_next().text)
data.append(item)