url = 'https://www.allrecipes.com/recipes/695/world-cuisine/asian/chinese/'
url2 = 'https://www.allrecipes.com/recipes/94/soups-stews-and-chili/'
new = url.split("/")[-4:]
new2 = url2.split("/")[-2:]
print(new)
print(new2)
Output : ['world-cuisine', 'asian', 'chinese', '']
['soups-stews-and-chili', '']
URL的其他一些示例包括:
"https://www.allrecipes.com/recipes/416/seafood/fish/salmon/"
"https://www.allrecipes.com/recipes/205/meat-and-poultry/pork/"
我们如何编写规则来遵循此类URL的分页https://www.allrecipes.com/recipes/695/world-cuisine/asian/chinese/?page=2"
规则(LinkExtractor(allow=(r'recipes/?page=\d+),follow=True)
我对scrapy和regex是新手,因此我非常感谢您在这个问题上的帮助
我不能100%确定我是否正确理解了您的问题,但我认为下面的代码可以满足您的需要
编辑
注释交互后更新的代码
输出
旧答案
输出
像这样的。其思想是找到“int”路径元素并从其右侧获取所有路径元素
输出
您可以组合
re
模块+str.split
:印刷品:
相关问题 更多 >
编程相关推荐