我用下面的程序得到url响应,但是只有下面的url不能正确给出响应。我等了很久,还是跑不完。我该如何解决?
import re
from bs4 import BeautifulSoup
import whois
import urllib
import urllib.request
from datetime import datetime
try:
url="http://zozo.jp/shop/bestpackingstore/?price=proper&p_ssy=2015&p_ssm=5&p_ssd=13&p_sey=2015&p_sem=5&p_sed=13&dstk=2"
header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
response = requests.get(url, headers=header)
print(response.text)
except:
response = ""
您的用户代理无效。
此代码显示了使用有效用户代理的改进模式
import requests
from bs4 import BeautifulSoup as BS
url = 'http://zozo.jp/shop/bestpackingstore'
params = {
'price': 'proper',
'p_ssy': 2015,
'p_ssm': 5,
'p_ssd': 13,
'p_sey': 2015,
'p_sem': 5,
'p_sed': 13,
'dstk': 2
}
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Safari/605.1.15'
}
with requests.get(url, headers=headers, params=params) as response:
response.raise_for_status()
soup = BS(response.text, 'lxml')