Python程序下载m3u列表:Post方法无法定位结果页求助
问题:Python POST请求无法获取M3U列表页面
我想用Python程序下载一份M3U列表,但通过POST方法无法找到对应的结果页面,相关页面URL为 https://playtvnow.com/shop1/order/trial-products/1,以下是我当前的代码:
from __future__ import print_function import requests , json, re,random,string,time,warnings from requests.packages.urllib3.exceptions import InsecureRequestWarning warnings.simplefilter('ignore',InsecureRequestWarning) post_url='https://playtvnow.com/shop1/modules/addons/LagomOrderForm/api/index.php/cart/registration-type/update' post_url2='https://playtvnow.com/shop1/modules/addons/LagomOrderForm/api/index.php/cart/billing-details/update' def get_clipmails(): Headers_={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0', 'Accept': 'application/json, text/plain, */*', 'Accept-Language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3', 'Content-Type':'application/x-www-form-urlencoded', 'Origin': 'https://playtvnow.com/', 'Connection': 'keep-alive', 'Referer': 'https://playtvnow.com/shop1/order/trial-products/1', 'Accept-Encoding':'gzip'}#, deflate, br'} params='details[firstname]='+NAME_TARGET+'&details[lastname]='+LAST_TARGET+'&details[email]='+EMAIL_TARGET+'&details[callingCode]=1&details[phonenumber]='+PHON_TARGET+'&details[companyname]=&details[tax_id]=&details[address1]='+ADRS_TARGET+'&details[address2]='+ADRS_TARGET+'&details[city]='+CITY_TARGET+'&details[state]='+STATE_TARGET+'&details[country]=US&details[password]='+RND_PASS+'&details[password2]='+RND_PASS+'&details[postcode]='+ZIP_TARGET+'&paymentMethod=mailin&details[marketingoptin]=1&details[country-calling-code-phonenumber]=1&acceptTos=¤cy=1&' post_data2=requests.post(post_url2,headers=Headers_,data=params).text print("POST DATA2:",post_data2) post_data=requests.post(post_url,headers=Headers_,data=params).text print("POST DATA:",post_data)
问题分析与解决步骤
1. 会话未保持
网站通常通过Cookie跟踪用户会话,当前代码每次POST都是独立请求,无法维持状态。改用requests.Session()统一管理Cookie和会话。
2. 缺少CSRF令牌验证
多数表单提交需要CSRF令牌,需先访问初始页面提取令牌,添加到请求头或参数中。
3. 请求顺序错误
需模拟浏览器操作流程:先访问目标页面,再按正确顺序调用API(通常先更新注册类型,再更新账单详情)。
4. 参数完整性与编码问题
手动拼接参数易出现编码错误,改用字典格式传递参数;acceptTos参数为空,需设置为1表示同意条款。
修改后的代码
from __future__ import print_function import requests, re, random, string, warnings from requests.packages.urllib3.exceptions import InsecureRequestWarning warnings.simplefilter('ignore', InsecureRequestWarning) BASE_URL = "https://playtvnow.com/shop1/order/trial-products/1" POST_URL_REG = 'https://playtvnow.com/shop1/modules/addons/LagomOrderForm/api/index.php/cart/registration-type/update' POST_URL_BILL = 'https://playtvnow.com/shop1/modules/addons/LagomOrderForm/api/index.php/cart/billing-details/update' def get_clipmails(): # 初始化会话,自动维护Cookie session = requests.Session() session.headers.update({ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0', 'Accept': 'application/json, text/plain, */*', 'Accept-Language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3', 'Origin': 'https://playtvnow.com/', 'Connection': 'keep-alive', 'Referer': BASE_URL, 'Accept-Encoding': 'gzip' }) # 先访问初始页面,获取Cookie和CSRF令牌 response = session.get(BASE_URL, verify=False) csrf_match = re.search(r'name="csrfToken" value="([^"]+)"', response.text) if csrf_match: csrf_token = csrf_match.group(1) session.headers.update({'X-CSRF-Token': csrf_token}) # 字典形式传递参数,自动处理编码 params = { 'details[firstname]': NAME_TARGET, 'details[lastname]': LAST_TARGET, 'details[email]': EMAIL_TARGET, 'details[callingCode]': '1', 'details[phonenumber]': PHON_TARGET, 'details[companyname]': '', 'details[tax_id]': '', 'details[address1]': ADRS_TARGET, 'details[address2]': ADRS_TARGET, 'details[city]': CITY_TARGET, 'details[state]': STATE_TARGET, 'details[country]': 'US', 'details[password]': RND_PASS, 'details[password2]': RND_PASS, 'details[postcode]': ZIP_TARGET, 'paymentMethod': 'mailin', 'details[marketingoptin]': '1', 'details[country-calling-code-phonenumber]': '1', 'acceptTos': '1', 'currency': '1' } # 按正确顺序请求API post_data_reg = session.post(POST_URL_REG, data=params, verify=False).text print("POST REG DATA:", post_data_reg) post_data_bill = session.post(POST_URL_BILL, data=params, verify=False).text print("POST BILL DATA:", post_data_bill) # 提取并下载M3U列表(需根据页面实际结构调整规则) m3u_response = session.get(BASE_URL, verify=False) m3u_match = re.search(r'https?://[^\s"]+\.m3u', m3u_response.text) if m3u_match: m3u_url = m3u_match.group(0) print("找到M3U链接:", m3u_url) m3u_content = session.get(m3u_url, verify=False).text with open('playlist.m3u', 'w') as f: f.write(m3u_content) print("M3U文件已保存") # 定义必填变量 NAME_TARGET = "Test" LAST_TARGET = "User" EMAIL_TARGET = f"test_{random.randint(1000,9999)}@example.com" PHON_TARGET = f"555{random.randint(1000000,9999999)}" ADRS_TARGET = "123 Test St" CITY_TARGET = "New York" STATE_TARGET = "NY" ZIP_TARGET = "10001" RND_PASS = ''.join(random.choices(string.ascii_letters + string.digits, k=12)) get_clipmails()
补充说明
- 若页面CSRF令牌位置不同,需调整正则表达式匹配规则。
- M3U链接提取逻辑需根据实际页面结构修改,若M3U通过API返回,需跟踪浏览器后续请求。
- 确保所有变量已正确定义,避免运行报错。
内容的提问来源于stack exchange,提问作者mino31
相关产品推荐
相关产品推荐

