网络爬虫的代码有什么问题?

2024-06-12 05:26:30 发布

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

所以,我基本上是想找到一个特定页面的链接(这里有10个)。一切正常,我甚至得到正确的索引(startlink,startquote,endquote)。但是,URL没有打印。为什么会这样?出什么事了?如果你能改正,我会很高兴的!你知道吗

def web():
n=0
a=open('quora.txt', 'r') #I've saved it as a txt file in my system
b=a.read()
startlink=0
while(n<10):
    startlink=b.find('<a href=', startlink+1)
    startquote=b.find('"', startlink)
    endquote=b.find('"', startquote)
    url=b[startquote+1:endquote]
    print url, startlink, startquote, endquote
    n=n+1

这是我得到的结果,只有指数。不,URL

4506 4514 4514
5308 5316 5316
5357 5365 5365
5472 5480 5480
5515 5523 5523
5588 5596 5596
5639 5647 5647
5723 5731 5731
6828 6836 6836
6867 6875 6875

Tags: txtweburl链接defve页面open
1条回答
网友
1楼 · 发布于 2024-06-12 05:26:30

结束引号的搜索应在开始引号位置后的一个字符开始:

def web():
    n = 0
    a = open('quora.txt', 'r') #I've saved it as a txt file in my system
    b = a.read()
    startlink = 0
    while (n < 10):
        startlink = b.find('<a href=', startlink + 1)
        startquote = b.find('"', startlink)
        endquote = b.find('"', startquote + 1)
        url = b[startquote + 1:endquote]
        print url, startlink, startquote, endquote
        n = n + 1

因为现在它也和endquote的startquote匹配

相关问题 更多 >