我希望能够抓取“到谷歌的链接”并从中打印出来:
<a href= "http://www.google.com">link to google</a>下面的代码能够抓取链接,但我不知道如何让它抓取正常的文本。
def handle_starttag(self, tag, attrs):
if tag == 'a':
self.anchor = True
if self.anchor == True:
for attr in attrs:
if attr[0] == 'href':
print(attr[1])发布于 2016-11-28 11:23:26
与handle_data一起使用
def handle_starttag(self, tag, attrs):
if tag == 'a':
self.anchor = True
def handle_data(self, data):
if self.anchor:
print('anchor data is:', data)
self.anchor = False这将进入a标记并将self.anchor设置为true,然后偶然发现数据(在标记中),如果最后一个标记是a,则将打印数据。无论如何,在这一轮之后,self.anchor将再次为假,防止多个假锚检测。
https://stackoverflow.com/questions/40842880
复制相似问题