Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何不刮相同的url两次?_Python_Beautifulsoup_Xampp_Python Requests_Export To Csv - Fatal编程技术网

Python 如何不刮相同的url两次?

Python 如何不刮相同的url两次?,python,beautifulsoup,xampp,python-requests,export-to-csv,Python,Beautifulsoup,Xampp,Python Requests,Export To Csv,我的总体目标是创建一个url数组/列表,这些url在网站url刮取过程中不会被刮取。逻辑如下面的代码示例所示 ('scrapy.py')的逻辑: 在('source')中打开url~>从('source')中的url中查找“a”标记~>在“a”标记中查找“href”~>如果文件('doneurls.py')中的“href”值不等于(!=)('done')~>则将不等于('done')的url写入文件('url.py') 我使用的代码是“scrapy.py”: from bs4 import Be

我的总体目标是创建一个url数组/列表,这些url在网站url刮取过程中不会被刮取。逻辑如下面的代码示例所示

('scrapy.py')的逻辑:

在('source')中打开url~>从('source')中的url中查找“a”标记~>在“a”标记中查找“href”~>如果文件('doneurls.py')中的“href”值不等于(!=)('done')~>则将不等于('done')的url写入文件('url.py')

我使用的代码是“scrapy.py”:

from bs4 import BeautifulSoup
import requests
import csv
import os
import sys
from os.path import dirname, join, abspath
sys.path.insert(0, abspath(join(dirname(__file__), '..')))
from doneurls import done


source = requests.get('http://localhost/index.php').text


soup = BeautifulSoup(source, 'lxml')
file = open('./url.py', 'a')
csv_writer = csv.writer(file)

 from html.parser import HTMLParser

 class MyHTMLParser(HTMLParser):

    def handle_starttag(self,tag,attrs):
        # Only parse the 'anchor' tag.
        if tag == "a":
           # Check the list of defined attributes.
             for name, value in attrs:
           # If href is defined, print it.
           if name == "href":
            if value != done:
                csv_writer.writerow('b="'+value+'"')



parser = MyHTMLParser()
parser.feed(source)
file.close()
index.php:

<a href="http://localhost/next.php">hello</a>
<a href="http://localhost/next3.php">hello</a>
<a href="http://localhost/next2.php">hello</a>
<a href="http://localhost/next1.php">hello</a>
<a href="http://localhost/1.php">hello</a>
<a href="http://localhost/2.php">hello</a>
<a href="http://localhost/3.php">hello</a>
这段代码似乎可以工作,它只忽略了我添加到doneurls.py中的一个url,并且工作得很好,但我想做的是添加一个url数组,这样做

done = {
"http://localhost/2.php",
"http://localhost/next1.php",
"http://localhost/next2.php"}

当我尝试以数组形式运行“done”时,根本不会跳过任何URL。我使用这段代码来尝试不必刮取我过去刮取的URL

如果我很了解这个问题,您可以使用以下方法查看找到的每个URL是否都已完成:

if value != done:
但问题是,上面只允许检查一个
done
url,而不是可能已完成的多个url。因此,如果
done
成为一个列表,您可以使用操作符(这里您需要
不在
中,因为我们想检查它是否不在那里):


作为旁注,Python中的列表是使用方括号创建的,因此,
done
应该类似于以下内容:

done = [
    "http://localhost/2.php",
    "http://localhost/next1.php",
    "http://localhost/next2.php"
]

大括号代表s,尽管在这里设置
done
并不重要。

请描述您的问题,这不是代码,而是代码格式。问题正在更改中。也许新的信息提供了更多的澄清。
if value not in done:
done = [
    "http://localhost/2.php",
    "http://localhost/next1.php",
    "http://localhost/next2.php"
]