Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3 urllib.request通常可以';不要翻页_Python_Beautifulsoup_Urllib - Fatal编程技术网

Python3 urllib.request通常可以';不要翻页

Python3 urllib.request通常可以';不要翻页,python,beautifulsoup,urllib,Python,Beautifulsoup,Urllib,所以,我尝试从S7航空公司网站获取机票价格(然后我想尝试借助机器学习预测价格变化) 这个小解析器获取带有价格的页面,然后在BeautifulSoup的帮助下查找所选日期的最低价格,然后将其保存在.csv文件中 请求“冷却”服务器之间间隔30秒。如果get 403错误,请等待120分钟并继续。每25个请求停止“冷却” 问题是,我经常得到404错误。我检查了浏览器中的一些链接,它们可以正常工作——页面已经打开,一切正常。当我尝试只打开一个链接时,解析器会不时地打开,但也经常返回404错误 我尝试添加

所以,我尝试从S7航空公司网站获取机票价格(然后我想尝试借助机器学习预测价格变化)

这个小解析器获取带有价格的页面,然后在BeautifulSoup的帮助下查找所选日期的最低价格,然后将其保存在.csv文件中

请求“冷却”服务器之间间隔30秒。如果get 403错误,请等待120分钟并继续。每25个请求停止“冷却”

问题是,我经常得到404错误。我检查了浏览器中的一些链接,它们可以正常工作——页面已经打开,一切正常。当我尝试只打开一个链接时,解析器会不时地打开,但也经常返回404错误

我尝试添加超时,但没有帮助

我是编程新手,可能会犯明显的错误

先谢谢你。对不起我的英语:)


它是否会导致随机url上出现404?只有一个请求会定期导致404?@VadimKey随机url。这意味着,在一个周期中,一些url导致404,但在下一个周期中,相同的url正确工作,反之亦然。这对我来说非常奇怪(可能这就是S7防止爬行的方式。您是否查找QPXExpress API()?@VadimKey哇!这似乎是一个非常好的服务,适合我的目的!谢谢!它会导致随机url上的404吗?只有一个请求会定期导致404吗?@VadimKey随机url。这意味着,在一个周期中,一些url会导致404,但在下一个周期中,相同的url正确工作,反之亦然。这对我来说非常奇怪(可能这就是S7防止爬行的方式。您是否查找QPX Express API()?@VadimKey哇!这似乎是非常好的服务,适合我的目的!谢谢!
from bs4 import BeautifulSoup as BS
import urllib.request as req
import re
import time
from urllib.error import HTTPError


def loader(dest, dest_large, dep_date):
    url = "http://travelwith.s7.ru/selectExactDateSearchFlights.action?TA=1&TC=0&TI=0&" \
          "CUR=RUB&FLC=1&FLX=false&RDMPTN=false&SC1=ANY&FSC1=1&DD1=2016-%s&" \
          "DA1=SVX&DP1=AIR_SVX_RU&AA1=%s&AP1=AIR_%s&LAN=ru" % (dep_date, dest, dest_large)
    request = req.Request(url,
                          headers={
                              'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0'})
    avia = req.urlopen(request).read()
    soup = BS(avia, 'html.parser')
    if len([re.sub('[^0-9]', '', p.get_text()) for p in soup.find_all("span", class_="radiobutton-text")]) > 0:
        price = min([re.sub('[^0-9]', '', p.get_text()) for p in soup.find_all("span", class_="radiobutton-text")])
    else:
        price = 0
    with open('prices/SVX_%s_%s.csv' % (dest, dep_date), 'a', encoding="utf8") as file:
        file.write(str(time.time())+';'+str(price)+'\n')
    print(date, destination, "OK!",price)


dep_date = ['06-08', '06-15', '06-22', '06-29', '07-01', '07-08', '07-15',
            '07-22', '07-29', '08-01', '08-08', '08-15', '08-22', '08-29',
            '09-01', '09-08', '09-15', '09-22', '09-29', '10-01', '10-08', '10-15']
dest = ['FCO', 'PRG', 'BCN', 'TXL']
dest_large = ['FCO_IT', 'PRG_CZ', 'BCN_ES', 'TXL_DE']

counter = 0
while True:
    for date in dep_date:
        for i, destination in enumerate(dest):
            counter += 1
            try:
                loader(destination, dest_large[i], date)
                time.sleep(30)
            except req.HTTPError as err:
                if err.code == 403: # wait if IP has been banned
                    print(date, destination, "Error 403. Ban, wait 120 minutes!")
                    time.sleep(120 * 60)
                if err.code == 404:
                    print(date, destination, "404. Can't open!")
                    time.sleep(30)
            if counter == 25:
                time.sleep(60 * 35)
                counter = 0