美文网首页
Python爬虫基本使用(基于requests库)

Python爬虫基本使用(基于requests库)

作者: williamhlw | 来源:发表于2020-06-02 11:47 被阅读0次

抓取知乎发现页面

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)

相关文章

网友评论

      本文标题:Python爬虫基本使用(基于requests库)

      本文链接:https://www.haomeiwen.com/subject/qktuzhtx.html