2016-03-01 69 views
0

我想輸入一個網址列表,並抓住pn2和main1。我可以在不導入文件的情況下運行它,所以我知道它可行,但我不知道如何處理導入。這是我最近嘗試過的,下面是一小部分網址。提前致謝。python美麗湯進口網址

import urllib 
import urllib.request 
import csv 
from bs4 import BeautifulSoup 

csvfile = open("ecco1.csv") 
csvfilelist = csvfile.read() 
theurl="csvfilelist" 

soup = BeautifulSoup(theurl,"html.parser") 
for row in csvfilelist: 

    for pn in soup.findAll('td',{"class":"productText"}): 
     pn2.append(pn.text) 
    for main in soup.find_all('div',{"class":"breadcrumb"}): 
     main1 = main.text 

     print (main1) 
     print ('\n'.join(pn2)) 

的url: http://www.eccolink.com/products/productresults.aspx?catId=2458 http://www.eccolink.com/products/productresults.aspx?catId=2464 http://www.eccolink.com/products/productresults.aspx?catId=2435 http://www.eccolink.com/products/productresults.aspx?catId=2446 http://www.eccolink.com/products/productresults.aspx?catId=2463

+0

你有什麼問題?也許你想'csvfile.readlines()' – maxymoo

+0

我沒有收到錯誤,但它完成沒有結果 – PatrickP76

+0

嘗試csvfile.readlines(),但仍然沒有結果 – PatrickP76

回答

2

從我所看到的,你打開一個CSV文件並使用BeautifulSoup解析它。 這不應該是這樣。 BeautifulSoup解析html文件,而不是CSV。

看着你的代碼,如果你將html代碼傳遞給Bs4,看起來是正確的。

from bs4 import BeautifulSoup 
import requests 
links = [] 
file = open('links.txt') 
html = requests.get('http://www.example.com') 
soup = BeautifulSoup(html, 'html.parser') 
for x in soup.find_all('a',"class":"abc"): 
     links.append(x) 
     file.write(x) 
file.close() 

上面是一個非常基本的實現,我怎麼能在HTML代碼中得到一個目標元素,並將其寫入文件/或追加到一個列表。使用請求而不是urllib。這是一個更好的圖書館,更現代化。

如果你想輸入你的數據爲CSV,我最好的選擇是使用csv閱讀器作爲導入。

希望有所幫助。