使用python爬虫库requests,urllib爬取今日头条街拍美图
代码均有注释
import re,json,requests,os
from hashlib import md5
from urllib.parse import urlencode
from requests.exceptions import RequestException
from bs4 import BeautifulSoup
from multiprocessing import Pool
#请求索引页
def get_page_index(offset,keyword):
#传送的数据
data={
'offset': offset,
'format': 'json',
'keyword': keyword,
'autoload': 'true',
'count': '20',
'cur_tab': 1
}
#自动编码为服务器可识别的url
url="https://www.toutiao.com/search_content/?"+urlencode(data)
#异常处理
try:
#获取返回的网页
response=requests.get(url)
#判断网页的状态码是否正常获取
if response.status_code==200:
#返回解码后的网页
return