程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python collects beauty content. Come and learn to download all your favorite content~

編輯:Python

Catalog ( You can click on the place you want to see )

      • This code is provided by : Qingdeng Education - Teacher Siyue
  • Knowledge point :
  • development environment :
    • If installed python Third-party module :
    • How to configure pycharm Inside python Interpreter ?
    • pycharm How to install plug-ins ?
  • Basic ideas and processes ( Universal )
  • Code
      • The import module
      • Add camouflage
      • 1. Send a request
      • 2. get data
  • Tail language


This code is provided by : Qingdeng Education - Teacher Siyue


Knowledge point :

  • Dynamic packet capture
  • requests Send a request
  • json Data analysis

development environment :

Run code

  • python 3.8

Auxiliary knock code

  • pycharm 2021.2

Third-party module

  • requests

If installed python Third-party module :

  1. win + R Input cmd Click ok , Enter the installation command pip install Module name (pip install requests) enter
  2. stay pycharm Click on the Terminal( terminal ) Enter the installation command

How to configure pycharm Inside python Interpreter ?

  1. choice file( file ) >>> setting( Set up ) >>> Project( project ) >>> python interpreter(python Interpreter )

  2. Click on the gear , choice add

  3. add to python The installation path


pycharm How to install plug-ins ?

  1. choice file( file ) >>> setting( Set up ) >>> Plugins( plug-in unit )

  2. Click on Marketplace Enter the name of the plug-in you want to install such as : Translation plug-ins Input translation / Chinese plug-in Input Chinese

  3. Select the corresponding plug-in and click install( install ) that will do

  4. After successful installation Yes, it will pop up restart pycharm The option to Click ok , Restart to take effect


Basic ideas and processes ( Universal )

  1. Send a request
  2. get data
  3. Parsing data
  4. Save the data

Code

The import module

import requests # Send a request Visit website
import re

Add camouflage

url = 'https://www.\.com/graphql'
# camouflage
headers = {

'content-type': 'application/json',
'Cookie': 'kpf=PC_WEB; kpn=KUAISHOU_VISION; clientid=3; did=web_ea128125517a46bd491ae9ccb255e242; client_key=65890b29; didv=1646739254078; _bl_uid=pCldq3L00L61qCzj6fytnk2wmhz5; userId=270932146; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABJBZGcI4Czt3EqaG90aWFm2EPPYcAQlkuV3ZOkHUBcqEtWV--udx6stXFOhEkGgx4tNCBS9Vhl-GstWLkvn-r_eV1072IPsO8d5sUcUuTJv3nicPWVBcfHW813ST2a4uN7HyHsnpnRkjx2BFXoMRmdSO4tbAgy3-3QRTaw05tiEp79qGRfQgVmK4kVJLOTF9X7o9vSjrLrTaRxEf0rwsXJhoSsmhEcimAl3NtJGybSc8y6sdlIiCkLEL3ZiZwp85TGjXIHaw7KGda_VNpfdZ1qigsOkLmESgFMAE; kuaishou.server.web_ph=ad8a744eb59aab3bf509625671ad16837e66',
'Host': 'www.\.com',
'Origin': 'https://www.\.com',
'Referer': 'https://www.\.com/search/video?searchKey=%E5%8F%8C%E9%A9%AC%E5%B0%BE',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
}
# The data transfer
for page in range(1, 10):
json = {

'operationName': "visionSearchPhoto",
'query': "fragment photoContent on PhotoEntity {\n id\n duration\n caption\n likeCount\n viewCount\n realLikeCount\n coverUrl\n photoUrl\n photoH265Url\n manifest\n manifestH265\n videoResource\n coverUrls {\n url\n __typename\n }\n timestamp\n expTag\n animatedCoverUrl\n distance\n videoRatio\n liked\n stereoType\n profileUserTopPhoto\n __typename\n}\n\nfragment feedContent on Feed {\n type\n author {\n id\n name\n headerUrl\n following\n headerUrls {\n url\n __typename\n }\n __typename\n }\n photo {\n ...photoContent\n __typename\n }\n canAddComment\n llsid\n status\n currentPcursor\n __typename\n}\n\nquery visionSearchPhoto($keyword: String, $pcursor: String, $searchSessionId: String, $page: String, $webPageArea: String) {\n visionSearchPhoto(keyword: $keyword, pcursor: $pcursor, searchSessionId: $searchSessionId, page: $page, webPageArea: $webPageArea) {\n result\n llsid\n webPageArea\n feeds {\n ...feedContent\n __typename\n }\n searchSessionId\n pcursor\n aladdinBanner {\n imgUrl\n link\n __typename\n }\n __typename\n }\n}\n",
'variables': {
'keyword': " dual horsetail ", 'pcursor': str(page), 'page': "search", 'searchSessionId': "MTRfMjcwOTMyMTQ2XzE2NTU3MjcyNTU3NjFf5Y-M6ams5bC-XzUwMDQ"}
}

1. Send a request

 response = requests.post(url=url, headers=headers, json=json)

2. get data

<Response [200]>: The request is successful

 feeds = response.json()['data']['visionSearchPhoto']['feeds']
for feed in feeds:
author_id = feed['author']['id']
photo_id = feed['photo']['id']
print(author_id, photo_id)
caption = feed['photo']['caption']
photoUrl = feed['photo']['photoUrl']
print(caption, photoUrl)
caption = re.sub('[\\/:"?<>|\\n]', '', caption)
json_like = {

Source code 、 answer 、 Tutorial can be added Q skirt :832157862 Free collection
'operationName': "visionVideoLike",
'query': "mutation visionVideoLike($photoId: String, $photoAuthorId: String, $cancel: Int, $expTag: String) {\n visionVideoLike(photoId: $photoId, photoAuthorId: $photoAuthorId, cancel: $cancel, expTag: $expTag) {\n result\n __typename\n }\n}\n",
'variables': {

'cancel': 0,
'expTag': "1_i/2005246154318902369_xpcwebsearchxxnull0",
'photoAuthorId': author_id,
'photoId': photo_id
}
}
resp_ = requests.post(url=url, headers=headers, json=json_like)
# video_data = requests.get(photoUrl).content
# with open(f'video/{caption}.mp4', mode='wb') as f:
# f.write(video_data)

Tail language

Okay , My article ends here !

There are more suggestions or questions to comment on or send me a private letter ! Come on together and work hard (ง •_•)ง

If you like, just pay attention to the blogger , Or like the collection and comment on my article !!!


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved