我需要一些 Python 循环技术方面的帮助。找了几天,放弃了。。。系统:Windows(蟒蛇)想法:“我创建了一个 HTML 解析器脚本,但是由于缺少 Python 脚本的知识和经验,看来我需要在每个页面上都运行它。我无法修复它,这就是为什么我决定循环这个脚本并制作它为 100 页运行 100 次”....但结果,我找不到正确的方法来做到这一点......我的剧本import requestsimport pandas as pdimport urllib.parseimport urllib.requestimport reimport osimport sys url = "*******************/store/index.php" querystring ={"id":"***","act":"search","***":"***","country":"", "state":"*","city":"","zip":"","type":"","base":"","PAGENUM":"2"} headers = { 'Host': "www.*****", 'Connection': "keep-alive", 'Upgrade-Insecure-Requests': "1", 'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36",'Accept':"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",'Referer': "h************/store/index.php?id=********************&pagenum=2",'Accept-Encoding': "gzip, deflate",'Accept-Language': "en-US,en;q=0.9",'Cookie': "php_session_id_real=**********; cookname=**********; cook******",'cache-control': "no-cache",'Postman-Token': "**************************"} response = requests.request("GET", url, headers=headers,params=querystring) df_list = pd.read_html(response.text) df = df_list[-1] print(df)我需要更改的是 PAGENUM 查询字符串(例如:&pagenum=2,3,10,50等...)是否可以运行此 python 脚本X次,并且每次更改pagenum = pagenum + 1的值 ?希望得到您的建议!
2 回答
当年话下
TA贡献1890条经验 获得超9个赞
使用 afor并迭代到包含所有所需值的列表。接下来,用于str将值存储在字典中。
做这个:
import requests
import pandas as pd
import urllib.parse
import urllib.request
import re
import os
import sys
pagenums=[2,3,10,50]
#or pagenums = np.range(1,101)
for page in pagenums:
querystring ={"id":"***","act":"search","***":"***","country":"",
"state":"*","city":"","zip":"","type":"","base":"","PAGENUM":str(page)}
#......
#..... # more code here
#headers = {....}
对于每次迭代,PAGENUMkey的值都会更新。
添加回答
举报
0/150
提交
取消