Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.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
使用python刮取零件时跳过列表中的链接_Python_Pandas_Beautifulsoup - Fatal编程技术网

使用python刮取零件时跳过列表中的链接

使用python刮取零件时跳过列表中的链接,python,pandas,beautifulsoup,Python,Pandas,Beautifulsoup,在这里的第一篇文章中,我无数次遇到已经解决但无法解决的问题 下面的while循环旨在下载URL列表中包含的文本(示例中为3)。它对所有的链接都是这样做的,但是这个链接(),这个例子中的第三个,需要很长时间。通过在浏览器中打开它,它看起来好像有许多jpg图像。(txt表示合并到一个文件中的多个文档;在这个特定示例中,一些文档是图像) 由于前面的这些行,可以在文本中识别图像: <DOCUMENT> <TYPE>GRAPHIC <SEQUENCE>7 <FIL

在这里的第一篇文章中,我无数次遇到已经解决但无法解决的问题

下面的while循环旨在下载URL列表中包含的文本(示例中为3)。它对所有的链接都是这样做的,但是这个链接(),这个例子中的第三个,需要很长时间。通过在浏览器中打开它,它看起来好像有许多jpg图像。(txt表示合并到一个文件中的多个文档;在这个特定示例中,一些文档是图像)

由于前面的这些行,可以在文本中识别图像:

<DOCUMENT>
<TYPE>GRAPHIC
<SEQUENCE>7
<FILENAME>tex99-4_pg01.jpg
<DESCRIPTION>GRAPHIC
<TEXT>
begin 644 tex99-4_pg01.jpg

图解的
7.
tex99-4_pg01.jpg
图解的
开始644 tex99-4_pg01.jpg
它们后面跟着这个代码:

end
</TEXT>
</DOCUMENT>
结束
如果下载时间太长,为了使刮板更快,有没有办法跳过该链接?我期待着将这段代码应用到这些链接中的320K,并希望找到一种方法使下载速度更快,而不是“剪切”我之后得到的txt

这是我目前用来刮的东西:

import pandas as pd
import requests

list_of_links = ['https://www.sec.gov/Archives/edgar/data/1000298/0001193125-14-321757.txt',  
                 'https://www.sec.gov/Archives/edgar/data/1002225/0000903423-14-000495.txt', 
                 'https://www.sec.gov/Archives/edgar/data/1004724/0001144204-14-042745.txt'] # the one with the images
number_of_urls = len(list_of_links)  # get number of links to iterate them
i = 0         

column_names = ["Filing text"]
DF = pd.DataFrame(columns = column_names)

while i < number_of_urls: 
    print("File #", i+1,"\tis being processed") # print this to visually see how long each download takes
    DF.loc[i, "Filing text"] = requests.get(list_of_links[i]).text
    i += 1
将熊猫作为pd导入
导入请求
链接列表=['https://www.sec.gov/Archives/edgar/data/1000298/0001193125-14-321757.txt',  
'https://www.sec.gov/Archives/edgar/data/1002225/0000903423-14-000495.txt', 
'https://www.sec.gov/Archives/edgar/data/1004724/0001144204-14-042745.txt“]#有图像的那个
url的数量=len(链接的列表)#获取链接的数量以迭代它们
i=0
列名称=[“归档文本”]
DF=pd.DataFrame(列=列名称)
而我
对于
请求
库,您可以检查以下答案:

在您的情况下,因为它始终只是文本数据,所以查看内容长度就足够了

另一方面,在请求调用中包含
timeout
始终是一种好的做法。这并不能解决您的问题,因为超时只考虑服务器不应答的时间,而不应答会产生问题,特别是在循环中

编辑

这可能是您问题的有效解决方案(我也将Paul H的反馈纳入其中):

请注意,我交换了列表中的第二个和最后一个URL,因此您可以更好地评估节省的时间。此外,请记住,如果df在末尾,您将有一些
None
,并根据要下载的数据设置有意义的
CONTENT\u LENGTH\u LIMIT

import pandas as pd
import requests

list_of_links = ['https://www.sec.gov/Archives/edgar/data/1000298/0001193125-14-321757.txt',
                 'https://www.sec.gov/Archives/edgar/data/1004724/0001144204-14-042745.txt',  
                 'https://www.sec.gov/Archives/edgar/data/1002225/0000903423-14-000495.txt', 
                 ] # the one with the images     

column_names = ["Filing text"]
DF = pd.DataFrame(columns = column_names)

CONTENT_LENGTH_LIMIT = 8070869 # just a limit which is lower than the size of the third link
def fetch_text(url):
    print("Url", url,"\tis being processed") # print this to visually see how long each download takes
    try:
        r = requests.get(url, stream=True, timeout=5)
        if int(r.headers.get('Content-Length')) > CONTENT_LENGTH_LIMIT:
            print("Url", url,"\thas been skipped")
            return None
        else: 
            text = "".encode() # the chunks are in bytes
            for chunk in r.iter_content(1024):
                text += chunk
        return text.decode()
    except requests.exceptions.Timeout as e:
        print("The server did not respond in time")
        # here it will return None, you can filter it later

DF = pd.DataFrame([fetch_text(link) for link in list_of_links],
                    columns=column_names)

对于
请求
库,您可以检查以下答案:

在您的情况下,因为它始终只是文本数据,所以查看内容长度就足够了

另一方面,在请求调用中包含
timeout
始终是一种好的做法。这并不能解决您的问题,因为超时只考虑服务器不应答的时间,而不应答会产生问题,特别是在循环中

编辑

这可能是您问题的有效解决方案(我也将Paul H的反馈纳入其中):

请注意,我交换了列表中的第二个和最后一个URL,因此您可以更好地评估节省的时间。此外,请记住,如果df在末尾,您将有一些
None
,并根据要下载的数据设置有意义的
CONTENT\u LENGTH\u LIMIT

import pandas as pd
import requests

list_of_links = ['https://www.sec.gov/Archives/edgar/data/1000298/0001193125-14-321757.txt',
                 'https://www.sec.gov/Archives/edgar/data/1004724/0001144204-14-042745.txt',  
                 'https://www.sec.gov/Archives/edgar/data/1002225/0000903423-14-000495.txt', 
                 ] # the one with the images     

column_names = ["Filing text"]
DF = pd.DataFrame(columns = column_names)

CONTENT_LENGTH_LIMIT = 8070869 # just a limit which is lower than the size of the third link
def fetch_text(url):
    print("Url", url,"\tis being processed") # print this to visually see how long each download takes
    try:
        r = requests.get(url, stream=True, timeout=5)
        if int(r.headers.get('Content-Length')) > CONTENT_LENGTH_LIMIT:
            print("Url", url,"\thas been skipped")
            return None
        else: 
            text = "".encode() # the chunks are in bytes
            for chunk in r.iter_content(1024):
                text += chunk
        return text.decode()
    except requests.exceptions.Timeout as e:
        print("The server did not respond in time")
        # here it will return None, you can filter it later

DF = pd.DataFrame([fetch_text(link) for link in list_of_links],
                    columns=column_names)

哦,老兄,不要像那样附加到数据帧中。在实例化中后期填充:
pandas.DataFrame([requests.get(link).text for link in list\u of\u links],columns=columns\u names)
oh man--不要像这样附加到数据框中。在实例化中后期填充:
pandas.DataFrame([requests.get(link).text for link in list\u of\u links],columns=columns\u name)
谢谢-非常好的资料。我会把它包括在内,看看我能不能solve@arabinelli-谢谢-看起来棒极了。很高兴能帮上忙!谢谢你-很好的材料。我会把它包括在内,看看我能不能solve@arabinelli-谢谢-看起来棒极了。很高兴能帮上忙!