在python中解析Robots.txt
我想用python解析robots.txt文件。 我已经研究了robotParser和robotExclusionParser,但没有什么真正满足我的标准。我想在一次快照中获取所有DialLowedURL和AllowedURL,而不是手动检查每个url是否允许。有任何库可以执行此操作吗?您可以使用curl命令将robots.txt文件读入一个字符串,并使用新行检查允许和不允许URL将其拆分在python中解析Robots.txt,python,robots.txt,Python,Robots.txt,我想用python解析robots.txt文件。 我已经研究了robotParser和robotExclusionParser,但没有什么真正满足我的标准。我想在一次快照中获取所有DialLowedURL和AllowedURL,而不是手动检查每个url是否允许。有任何库可以执行此操作吗?您可以使用curl命令将robots.txt文件读入一个字符串,并使用新行检查允许和不允许URL将其拆分 import os result = os.popen("curl https://fortune.com
import os
result = os.popen("curl https://fortune.com/robots.txt").read()
result_data_set = {"Disallowed":[], "Allowed":[]}
for line in result.split("\n"):
if line.startswith('Allow'): # this is for allowed url
result_data_set["Allowed"].append(line.split(': ')[1].split(' ')[0]) # to neglect the comments or other junk info
elif line.startswith('Disallow'): # this is for disallowed url
result_data_set["Disallowed"].append(line.split(': ')[1].split(' ')[0]) # to neglect the comments or other junk info
print (result_data_set)
您可以使用curl命令将robots.txt文件读入一个字符串,并使用新行检查允许和不允许URL将其拆分
import os
result = os.popen("curl https://fortune.com/robots.txt").read()
result_data_set = {"Disallowed":[], "Allowed":[]}
for line in result.split("\n"):
if line.startswith('Allow'): # this is for allowed url
result_data_set["Allowed"].append(line.split(': ')[1].split(' ')[0]) # to neglect the comments or other junk info
elif line.startswith('Disallow'): # this is for disallowed url
result_data_set["Disallowed"].append(line.split(': ')[1].split(' ')[0]) # to neglect the comments or other junk info
print (result_data_set)
为什么要手动检查URL? 您可以在Python3中使用urllib.robotparser,并执行如下操作
import urllib.robotparser as urobot
import urllib.request
from bs4 import BeautifulSoup
url = "example.com"
rp = urobot.RobotFileParser()
rp.set_url(url + "/robots.txt")
rp.read()
if rp.can_fetch("*", url):
site = urllib.request.urlopen(url)
sauce = site.read()
soup = BeautifulSoup(sauce, "html.parser")
actual_url = site.geturl()[:site.geturl().rfind('/')]
my_list = soup.find_all("a", href=True)
for i in my_list:
# rather than != "#" you can control your list before loop over it
if i != "#":
newurl = str(actual_url)+"/"+str(i)
try:
if rp.can_fetch("*", newurl):
site = urllib.request.urlopen(newurl)
# do what you want on each authorized webpage
except:
pass
else:
print("cannot scrap")
为什么要手动检查URL? 您可以在Python3中使用urllib.robotparser,并执行如下操作
import urllib.robotparser as urobot
import urllib.request
from bs4 import BeautifulSoup
url = "example.com"
rp = urobot.RobotFileParser()
rp.set_url(url + "/robots.txt")
rp.read()
if rp.can_fetch("*", url):
site = urllib.request.urlopen(url)
sauce = site.read()
soup = BeautifulSoup(sauce, "html.parser")
actual_url = site.geturl()[:site.geturl().rfind('/')]
my_list = soup.find_all("a", href=True)
for i in my_list:
# rather than != "#" you can control your list before loop over it
if i != "#":
newurl = str(actual_url)+"/"+str(i)
try:
if rp.can_fetch("*", newurl):
site = urllib.request.urlopen(newurl)
# do what you want on each authorized webpage
except:
pass
else:
print("cannot scrap")
我可以问一下robot.txt包含什么,以及解析文本文件是什么意思吗?robots.txt是每个站点地图支持都遵循的标准。Sitemap:使我们的内容可搜索。示例:啊,好的,现在更有意义了,也许你应该在你的问题中为不熟悉这个概念的其他人链接到这个。由于robot.txt数据在标记中,你不能在这里使用html解析,这里有一个替代选项disallow=[i for i in data.split'\n'如果i中的'disallow'我可以问一下robot.txt包含什么,以及解析文本文件是什么意思吗?robots.txt是每个站点地图支持都遵循的标准。Sitemap:使我们的内容可搜索。示例:啊,好的,现在更有意义了,也许你应该在你的问题中为不熟悉这个概念的其他人链接到这个。由于robot.txt数据在标记中,你不能在这里使用html解析,这里有一个替代选项disallow=[i for i in data.split'\n'如果i中的'disallow'不客气。不@Ritu,找不到满足您的用例的。也许你可以扩展它并建立图书馆。欢迎你。不@Ritu,找不到满足您的用例的。也许您可以扩展它并构建库。