抓取知乎发现页面
import requests
import re
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
}
r = requests.get('https://www.zhihu.com/explore',headers=headers) #设置header 模拟浏览器请求
pattern = re.compile('ExploreRoundtableCard-intro.*?data-za-detail-view-id.*?>(.*?)</a>', re.S) #正则匹配标题
titles = re.findall(pattern, r.text)
print(titles)
抓取github图标并保存
r = requests.get('https://github.com/favicon.ico')
with open('favicon.ico', 'wb') as f:
f.write(r.content)
发送post请求
data = {'name': 'germey', 'age': '22'}
r = requests.post("http://httpbin.org/post", data=data)
print(r.text)
网友评论