2017-04-09 83 views
0

练习中,我一直在用BeautifulSoup学习Python和网页抓取。我正在寻找一个可以在网站上找到团队页面的程序,并且可以删除团队成员的姓名。以下是“团队”页面的示例:http://plasticbank.org/team-speakers/使用BeautifulSoup查找“团队”页面

我已经认识到,所有团队页面都有“团队”显着更大,但并非所有网站都使用标题,因此很难通过它们进行解析。我已经得到尽可能加载与urllib2的URL。我如何浏览一个网站的主页并找到一个“团队”或真正的任何具有特定主题的页面?这与寻找联系页面的问题是一样的,你如何告诉刮板找到它?

这里是我的代码完成的部分:(这只是用来加载网站)

#Pre: url is a string containing the address of a website 
#return: A string with the URL formatted to include http:// 
def ensureurl(url): 
    if '//' not in url: 
     return "http://" + url 
    else: 
     return url 

#Pre: url is a string containing the address of a website 
#return: The HTML code at that URL or an empty string if the URL could not be processed 
def read_url(url): 
    url = ensureurl(url) 
    print url 

    try: 
     #User agent spoofing to trick sites into thinking the bot is a human. 
     #This does not work on all sites. 
     hdr = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0', 
     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
     'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 
     'Accept-Encoding': 'none', 
     'Accept-Language': 'en-US,en;q=0.8', 
     'Connection': 'keep-alive'} 
     request = urllib2.Request(url, headers=hdr) 
     return urllib2.urlopen(request).read() 
    except urllib2.HTTPError, e: 
     print e.fp.read() 
     return "" 
+0

你写的任何代码? – elena

+0

我刚刚编辑帖子以包含我的代码的完整部分 –

回答

0

铲运机没有找到它自己的东西 - 你需要描述定义你的技术术语”重新寻找,这意味着你将不得不建立一些规则来定义'团队'页面是什么。

作为一个经验法则,为了能够使用BeautifulSoup来识别你应该能够通过查看它的HTML来识别它。

在你的特殊情况下,这是一个相当大的任务。 也许你可以从寻找'标题'标签开始?如果我是你,那就是我去的地方。

+0

这听起来像个好主意。假设刮刀可以找到“标题”标签并确认它是否为“团队”部分:如果刮刀检查的第一页没有“团队”部分,您会建议我怎么做? –