Issue
This code was provided for me, it gives back a lot of info about one post i want to use it to get the same info in this url and increase the posts number in this page now it's only 20
import requests
# https://haraj.com.sa/1179070147
def main(url):
params = {
'queryName': 'detailsPosts_singlePost',
'token': '',
'clientid': '812f41b2-9936-4405-aa9c-378db19b8cc4',
'version': '8.2.9 , 10 18 - 7 - 21'
}
data = {
"query": "query($ids:[Int]) { posts( id:$ids) {\n\t\titems {\n\t\t\tid status authorUsername title city postDate updateDate hasImage thumbURL authorId bodyHTML bodyTEXT city tags imagesList commentStatus commentCount upRank downRank geoHash\n\t\t}\n\t\tpageInfo {\n\t\t\thasNextPage\n\t\t}\n\t\t} }",
"variables": {
"ids": [
79070147
]
}
}
r = requests.post(url, params=params, json=data)
print(r.json())
main('https://graphql.haraj.com.sa/')
any help is appreciated thanks
Solution
Loop over the pages in order to obtain the desired information.
Note, you can pickup all information from the JSON response directly without needs to call the API again.
import requests
from pprint import pp
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0'
}
def main(url):
with requests.Session() as req:
req.headers.update(headers)
for page in range(1, 3): # increase the number of pages from here.
params = {
'queryName': 'detailsPosts_tag_page{}'.format(page),
'token': '',
'clientid': '812f41b2-9936-4405-aa9c-378db19b8cc4',
'version': '8.2.9 , 10 18 - 7 - 21'
}
data = {
"query": "query($tag:String,$page:Int) { posts( tag:$tag, page:$page) {\n\t\titems {\n\t\t\tid status authorUsername title city postDate updateDate hasImage thumbURL authorId bodyHTML bodyTEXT city tags imagesList commentStatus commentCount upRank downRank geoHash geoCity geoNeighborhood\n\t\t}\n\t\tpageInfo {\n\t\t\thasNextPage\n\t\t}\n\t\t} }",
"variables": {
"page": page,
"tag": "حراج العقار"
}
}
r = req.post(url, params=params, json=data)
if r.status_code == 200:
for i in r.json()['data']['posts']['items']:
pp(i)
# check i.keys()
else:
exit(f"Page# {page} is not exist, However program stopped.")
main('https://graphql.haraj.com.sa/')
Answered By - αԋɱҽԃ αмєяιcαη
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.