2016-07-18 61 views
0

でリンクとipsの両方をキャッチして、私はこのページのトピックのすべてのリンクをキャッチするスクリプトを作成しましたhttps://www.inforge.net/xi/forums/liste-proxy.1118/。これらのトピックには、プロキシのリストが含まれています。スクリプトはこれです:フォーラムの助けを借りて、python3

import urllib.request, re 
from bs4 import BeautifulSoup 

url = "https://www.inforge.net/xi/forums/liste-proxy.1118/" 
soup = BeautifulSoup(urllib.request.urlopen(url), "lxml") 

base = "https://www.inforge.net/xi/" 

for tag in soup.find_all("a", {"class":"PreviewTooltip"}): 
    links = tag.get("href") 
    final = [base + links] 

final2 = urllib.request.urlopen(final) 

for line in final2: 
    ip = re.findall("(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3}):(?:[\d]{1,5})", line) 
    ip = ip[3:-1] 

for addr in ip: 
    print(addr) 

出力は次のようになります。

Traceback (most recent call last): 
    File "proxygen5.0.py", line 13, in <module> 
    sourcecode = urllib.request.urlopen(final) 
    File "/usr/lib/python3.5/urllib/request.py", line 162, in urlopen 
    return opener.open(url, data, timeout) 
    File "/usr/lib/python3.5/urllib/request.py", line 456, in open 
    req.timeout = timeout 
AttributeError: 'list' object has no attribute 'timeout' 

私は問題はの一部であることを知っている:final2 = urllib.request.urlopen(final)が、私は

何缶を解決する方法がわかりません私はipsを印刷するのですか?このコードは、あなたが望む何をすべき

+1

問題は '一つの要素を持つリストを作成し、'最終的には= [ベース+リンク]ということで、そしてより'final2 = urllib.request.urlopen(final)'のリストを使用します。ここでは、リストではなく文字列(url)を渡す必要があります。 – Ceppo93

+0

ええ、私はばかです、あなたは正しいです。どうすればこれを回避できますか?もしあなたが – Sperly1987

+1

に答えることができるなら、 'final = [base + links]'を 'final = base + links'に置き換えてください。タグから解析した最後の_final_のみを保持していることに注意してください。必要な場合は、他のコードを変更する必要があります。 – Ceppo93

答えて

2

あなたはすべての通路を理解することができますので、それはコメントしています:

import urllib.request, re 
from bs4 import BeautifulSoup 

url = "https://www.inforge.net/xi/forums/liste-proxy.1118/" 
soup = BeautifulSoup(urllib.request.urlopen(url), "lxml") 

base = "https://www.inforge.net/xi/" 

# Iterate over all the <a> tags 
for tag in soup.find_all("a", {"class":"PreviewTooltip"}): 
    # Get the link form the tag 
    link = tag.get("href") 
    # Compose the new link 
    final = base + link 

    print('Request to {}'.format(final)) # To know what we are doing 
    # Download the 'final' link content 
    result = urllib.request.urlopen(final) 

    # For every line in the downloaded content 
    for line in result: 
     # Find one or more IP(s), here we need to convert lines to string because `bytes` objects are given 
     ip = re.findall("(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3}):(?:[\d]{1,5})", str(line)) 
     # If one ore more IP(s) are found 
     if ip: 
      # Print them on separate line 
      print('\n'.join(ip)) 
+2

あなたは天使です!あなたは私のような初心者を助けました。私はどのように私はあなたに感謝することができるかわからない...あなたは最高の – Sperly1987

+0

最後の質問ですし、私はやった..私はファイル内のすべてのキャッチipsを保存する場合は、どうすればいいですか?私は試してみました:out_file = 'open(" proxy.txt "、" w ")out_file.write(ip)out_file.close()'しかし、ipだけを保存します。 – Sperly1987

+2

ファイルを開くときに '' w "'の代わりに '' a "'を使うようにするには、 'append'モードでファイルを開くか、すべての内容を上書きする必要があります。 – Ceppo93

関連する問題