使用Python获取百度搜索结果url

2024-05-23 14:19:43 发布

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

我正在尝试从百度获得搜索结果。 但现在我被困在这里了:

import sys
import urllib
import urllib2
from bs4 import BeautifulSoup
question_word = "Hello"
url = "http://www.baidu.com/s?wd=" +     urllib.quote(question_word.decode(sys.stdin.encoding).encode('gbk'))
htmlpage = urllib2.urlopen(url).read()
soup = BeautifulSoup(htmlpage)
for child in soup.findAll("h3", {"class": "t"}):
    print child.contents[0]

这将返回具有目标URL的所有标记。 我不知道如何使用.get('href')列出实际的URL

我是Python新手,因此可能对基本概念有一些混淆。。 我真的很感激你的帮助。在

谢谢!在


Tags: fromimportchildurlsysurllib2urllibword
1条回答
网友
1楼 · 发布于 2024-05-23 14:19:43
for child in soup.findAll("h3", {"class": "t"}):
    print child.a.get('href')

使用.获得h3标记中的下一个a标记,然后可以使用.get()

相关问题 更多 >