日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python爬虫今日头条街拍美图开发背景_分析Ajax请求并抓取今日头条街拍美图:爬取详情页的url与实际页面上显示不符...

發布時間:2024/9/27 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬虫今日头条街拍美图开发背景_分析Ajax请求并抓取今日头条街拍美图:爬取详情页的url与实际页面上显示不符... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

from urllib.parse import urlencode

import re

from requests.exceptions import RequestException

from bs4 import BeautifulSoup

import requests

import json

def get_page_index(offset,keyword):#定義一個函數用于獲取索引頁信息

data = {

'offset': offset,

'format': 'json',

'keyword': keyword,

'autoload': 'true',

'count': '20',

'cur_tab': '3'

}

# cur_tab為3指的是圖集板塊,數過來第三個,若為1則指代綜合板塊

#count 數量

url = 'http://www.toutiao.com/search_content/?' + urlencode(data) #urlencode可將字典對象轉化為url的請求參數

headers = {

'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'

}

try:

response = requests.get(url,headers=headers)

if response.status_code == 200:

return response.text

else:

return None

except RequestException:

print('請求索引頁出錯')

return None

def parse_page_index(html):#定義一個函數解析索引頁信息,返回一個包含詳情頁url的迭代器

data = json.loads(html)#將字符串轉化為一個對象(字典)

if data and 'data' in data.keys():#判斷data是否為空,同時要滿足鍵里面有‘data’

for item in data.get('data'):#獲取字典中key為‘data’的對應的值,這個值data.get('data')為一個容量為20的列表,列表的元素為字典

yield item.get('article_url')#獲取字典中key為‘article_url’的對應的值,即網址

def get_page_detail(url):#定義一個函數用于得到詳情頁下的信息

try:

response = requests.get(url)

if response.status_code == 200:

return response.text

else:

return None

except RequestException:

print('請求詳情頁出錯')

return None

def parse_page_detail(html):#定義解析詳情頁的方法

soup = BeautifulSoup(html,'lxml')

title = soup.select('title')[0].get_text()

print(title)

'''

image_pattern = re.compile('gallery: (.*?),\n? ? siblingList',re.S)

result = re.search(image_pattern,html)

if result:

print(result.group(1))

'''

def main():

html = get_page_index('0','街拍')

for url in parse_page_index(html):#parse_page_index(html)返回的是一個迭代器,每次輸出一個網址

print(url)

html = get_page_detail(url)

if html:

parse_page_detail(html)

#print(url)

if __name__ == '__main__':

main()

總結

以上是生活随笔為你收集整理的python爬虫今日头条街拍美图开发背景_分析Ajax请求并抓取今日头条街拍美图:爬取详情页的url与实际页面上显示不符...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。