前言
本文介绍如何使用python爬取手机百度搜索结果的标题。
代码
# coding: utf-8
import urllib
import requests
from bs4 import BeautifulSoup
headersParameters = { # 发送HTTP请求时的HEAD信息,用于伪装为浏览器
'Connection': 'Keep-Alive',
'Accept': 'text/html, application/xhtml+xml, */*',
'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
'Accept-Encoding': 'gzip, deflate',
'User-Agent': 'Mozilla/6.1 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
}
keyword = "我的世界天骐新生活大冒险游戏"
url = u'https://m.baidu.com/s?word='+urllib.quote(keyword)
html = requests.get(url ,timeout=200, headers=headersParameters).text
print(html)
bs = BeautifulSoup(html)
for item in bs.find_all("h3"):
print(item.text)