Python:扫描网站的所有站点以查找特定的url

2024-04-27 05:17:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我想扫描我的特定链接论坛。所有链接都是这样:http://www.vbulletinxyz-forum.tld/forum/showthread.php?t=17590。只有链接末尾的线程编号更改。你知道吗

目前我正在使用以下代码,但它只适用于一个特定的网址,而不是论坛的所有线程。我要如何更改代码才能让它扫描所有线程?你知道吗

import urllib
mypath = "http://vbulletin-forumxyz.tld/forum/showthread.php?t=1"
mylines = urllib.urlopen(mypath).readlines()
for item in mylines:
    if "http://specific.tld" in item:
        print item[item.index("http://specific.tld"):]

Tags: 代码inhttp链接forumurllibitem线程
2条回答

这就是它的工作原理,它检查从0到400000的线程。你知道吗

import urllib.request
import time
import codecs

def mypath(t):
    return "http://www.someforum.org/forum/showthread.php?t={}".format(t)


for t in range(0,400000):
    conn = urllib.request.urlopen(mypath(t))

    # check status code
    if conn.getcode() != 200:
        continue

    mylines = conn.read().decode('windows-1251').splitlines()

    for item in mylines:
        if "http://someurl.tld" in item:
            print(item)

   # avoid fetching to fast (you might get banned otherwise)
   # time.sleep(0.5)
  1. 或者尝试所有的线程数
  2. 或者使用跟踪链接(并发现新线程)的spider

(1)易于实现,但可能并非所有线程号(t)都存在。所以会有很多404请求。你知道吗

(2)看看scrapy

更新(1):原则上可以这样做。请注意,a)您提供的url是不可访问的(虚拟的),所以我没有测试它,b)它的python3.X

import urllib.request
import time


def mypath(t):
    return "http://vbulletin-forumxyz.tld/forum/showthread.php?t={}".format(t)


for t in range(2):
    conn = urllib.request.urlopen(mypath(t))

    # check status code
    if conn.getcode() != 200:
        continue

    mylines = conn.read().decode('utf-8').splitlines()
    for item in mylines:
        if "http://specific.tld" in item:
            print(item)

   # avoid fetching to fast (you might get banned otherwise)
    time.sleep(0.5)

相关问题 更多 >