如何不抓取相同的 url 两次?
How to not scrape the same url twice?
我的总体目标是让 array/list 的 url 不会在网站期间被抓取 url 抓取逻辑如下面的代码示例所示
('scrapy.py') 的逻辑:
在 ('source') 中打开 url ~> 在 ('source') 中从 url 中找到 'a' 标签 ~> 找到 'href'在 'a' 标签中 ~> 如果 'href' 的值不等于文件 ('doneurls.py') 中的 (!=) ('done') ~> 然后写入 url不等于 ('done') 的 s 进入文件 ('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:
done = "http://localhost/2.php"
这段代码似乎有效,它只忽略了我添加到 doneurls.py 的一个 url 并且运行良好,但我想要做的是添加一个 url 的数组=]s 这样做
done = {
"http://localhost/2.php",
"http://localhost/next1.php",
"http://localhost/next2.php"}
当我尝试 运行 'done' 作为数组时,根本没有 url 被跳过。我正在使用此代码来尝试不必抓取我过去抓取过的 urls。
如果我很好地理解了这个问题,您正在尝试查看找到的每个 URL 是否已经完成,使用:
if value != done:
除了问题是上面只允许检查一个 done
url 而不是可能已经完成的多个 url。所以,如果 done
变成一个列表,你可以使用运算符 in
(这里你需要 not in
因为我们想检查它是否不存在):
if value not in done:
作为旁注,Python 中的列表是使用方括号创建的,因此,done
应该类似于这样的内容:
done = [
"http://localhost/2.php",
"http://localhost/next1.php",
"http://localhost/next2.php"
]
大括号适用于 sets and dictionaries,虽然 done
在这里并不重要。
我的总体目标是让 array/list 的 url 不会在网站期间被抓取 url 抓取逻辑如下面的代码示例所示
('scrapy.py') 的逻辑:
在 ('source') 中打开 url ~> 在 ('source') 中从 url 中找到 'a' 标签 ~> 找到 'href'在 'a' 标签中 ~> 如果 'href' 的值不等于文件 ('doneurls.py') 中的 (!=) ('done') ~> 然后写入 url不等于 ('done') 的 s 进入文件 ('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:
done = "http://localhost/2.php"
这段代码似乎有效,它只忽略了我添加到 doneurls.py 的一个 url 并且运行良好,但我想要做的是添加一个 url 的数组=]s 这样做
done = {
"http://localhost/2.php",
"http://localhost/next1.php",
"http://localhost/next2.php"}
当我尝试 运行 'done' 作为数组时,根本没有 url 被跳过。我正在使用此代码来尝试不必抓取我过去抓取过的 urls。
如果我很好地理解了这个问题,您正在尝试查看找到的每个 URL 是否已经完成,使用:
if value != done:
除了问题是上面只允许检查一个 done
url 而不是可能已经完成的多个 url。所以,如果 done
变成一个列表,你可以使用运算符 in
(这里你需要 not in
因为我们想检查它是否不存在):
if value not in done:
作为旁注,Python 中的列表是使用方括号创建的,因此,done
应该类似于这样的内容:
done = [
"http://localhost/2.php",
"http://localhost/next1.php",
"http://localhost/next2.php"
]
大括号适用于 sets and dictionaries,虽然 done
在这里并不重要。