美丽的汤解析URL得到另一个网址的数据 [英] Beautiful Soup to parse url to get another urls data
本文介绍了美丽的汤解析URL得到另一个网址的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要解析一个URL来获取链接到详细信息页面的URL列表。然后,从该网页我需要从该页面的所有细节。我需要做的是这种方式,因为在详细页的URL没有定期增加和变化,但该事件列表页面保持不变。
基本上是:
example.com/events/
&所述; A HREF =http://example.com/events/1>事件1所述; / A>
&所述; A HREF =http://example.com/events/2>事件2'; / A>example.com/events/1
......一些细节的东西,我需要example.com/events/2
......一些细节的东西,我需要
解决方案
进口的urllib2
从BeautifulSoup进口BeautifulSoup页= urllib2.urlopen('http://yahoo.com').read()
汤= BeautifulSoup(页)
汤。prettify()
在soup.findAll('A'中,href = TRUE)主播:
打印锚['href属性]
这会给你的URL列表。现在,您可以通过这些网址进行迭代,并分析数据。
-
inner_div = soup.findAll(格,{ID:Y-荫})
这是一个例子。你可以通过BeautifulSoup教程。
I need to parse a url to get a list of urls that link to a detail page. Then from that page I need to get all the details from that page. I need to do it this way because the detail page url is not regularly incremented and changes, but the event list page stays the same.
Basically:
example.com/events/
<a href="http://example.com/events/1">Event 1</a>
<a href="http://example.com/events/2">Event 2</a>
example.com/events/1
...some detail stuff I need
example.com/events/2
...some detail stuff I need
解决方案
import urllib2
from BeautifulSoup import BeautifulSoup
page = urllib2.urlopen('http://yahoo.com').read()
soup = BeautifulSoup(page)
soup.prettify()
for anchor in soup.findAll('a', href=True):
print anchor['href']
It will give you the list of urls. Now You can iterate over those urls and parse the data.
inner_div = soup.findAll("div", {"id": "y-shade"})
This is an example. You can go through the BeautifulSoup tutorials.
这篇关于美丽的汤解析URL得到另一个网址的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文