簡體   English   中英

scrapy 從 txt 文件讀取 url 失敗

[英]scrapy reading urls from a txt file fail

這就是 txt 文件的樣子,我從 jupiter notebook 打開它。 請注意,出於顯而易見的原因,我更改了結果中鏈接的名稱。 輸入 - - - - - - - - - - - - - - -

用 open('...\j.txt', 'r') as f: data = f.readlines()

打印(數據[0])打印(類型(數據))

輸出 - - - - - - - - - - - - - - - - -

[' https://www.example.com/191186976.html ', ' https://www.example.com/191187171.html ']

現在我在我的 scrapy 腳本中寫了這些,當我運行它時它沒有 go 的鏈接。 相反,它顯示:錯誤:獲取啟動請求時出錯。

class abc(scrapy.Spider): name = "abc_article"

with open('j.txt' ,'r')as f4:
    url_c = f4.readlines()

u = url_c[0]    
start_urls = u

如果我寫了 u = ['example.html', 'example.html'] starting_url = u 那么它工作得很好。 我是 scrapy 的新手,所以我想問一下這里有什么問題? 是閱讀方法還是我沒有注意到的其他東西。 謝謝。

這樣的事情應該讓你朝着正確的方向前進。

import csv
from urllib.request import urlopen
#import urllib2
from bs4 import BeautifulSoup

contents = []
with open('C:\\your_path_here\\test.csv','r') as csvf: # Open file in read mode
    urls = csv.reader(csvf)
    for url in urls:
        contents.append(url) # Add each url to list contents

for url in contents:  # Parse through each url in the list.
    page = urlopen(url[0]).read()
    soup = BeautifulSoup(page, "html.parser")
print(soup)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM