Webscraper将

2024-04-26 00:18:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经完全遵循了一个教程,我希望我的刮板刮出所有的链接到具体的网页,包含有关每个警察局的信息,但它几乎返回整个网站。

from urllib import urlopen
import re

f = urlopen("http://www.emergencyassistanceuk.co.uk/list-of-uk-police-stations.html").read()

b = re.compile('<span class="listlink-police"><a href="(.*)">')
a = re.findall(b, f)

listiterator = []
listiterator[:] = range(0,16)

for i in listiterator:
    print a 
    print "\n"

f.close()

Tags: fromimport刮板re信息网页网站链接
3条回答

使用BeautifulSoup

from bs4 import BeautifulSoup
from urllib2 import urlopen

f = urlopen("http://www.emergencyassistanceuk.co.uk/list-of-uk-police-stations.html").read()

bs = BeautifulSoup(f)

for tag in bs.find_all('span', {'class': 'listlink-police'}):
    print tag.a['href']

有超过1.6k的链接与该类在它上面。在

我认为它工作正常。。。你凭什么认为它不起作用?在


你一定要用漂亮的汤,它愚蠢、简单,而且非常有用。在

您正在使用regex解析HTML。你不应该,因为你最终只会遇到这种类型的问题。首先,.*通配符将尽可能多地匹配文本。但一旦你解决了这个问题,你就会从沮丧之树上摘下另一颗果实。改用一个合适的HTML解析器。在

相关问题 更多 >