Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 如何在选择器中使用部分文本而不是精确文本?_Python_Python 3.x_Web Scraping_Beautifulsoup_Css Selectors - Fatal编程技术网

Python 如何在选择器中使用部分文本而不是精确文本?

Python 如何在选择器中使用部分文本而不是精确文本?,python,python-3.x,web-scraping,beautifulsoup,css-selectors,Python,Python 3.x,Web Scraping,Beautifulsoup,Css Selectors,我用python编写了一个脚本,从torrent站点收集电影名称及其类型。由于BeautifulSoup不支持伪选择器,我找到了一种技术来解决这个问题。我现在面临的唯一问题是,要得到结果,下面脚本中倒逗号内的文本必须精确。是否有任何方法可以使用类似于部分匹配中的:contains属性的内容,以便即使查询中的文本包含部分单词,我仍将解析我要查找的类型。[预计在脚本中使用Gen或nre:或enr而不是流派:] 以下是脚本: import requests from bs4 import Beaut

我用python编写了一个脚本,从torrent站点收集电影名称及其类型。由于
BeautifulSoup
不支持伪选择器,我找到了一种技术来解决这个问题。我现在面临的唯一问题是,要得到结果,下面脚本中倒逗号内的文本必须精确。是否有任何方法可以使用类似于部分匹配中的
:contains
属性的内容,以便即使查询中的文本包含部分单词,我仍将解析我要查找的
类型。[预计在脚本中使用
Gen
nre:
enr
而不是
流派:
]

以下是脚本:

import requests 
from bs4 import BeautifulSoup

soup = BeautifulSoup(requests.get("https://www.yify-torrent.org/search/1080p/").text,"lxml")
for title in soup.select("div.mv"):
    names = title.select("h3 a")[0].text
    genre = ' '.join([item.next_sibling for item in title.select(".mdif li b") if item.text=="Genre:"])
    print(names, genre)
结果:

Swelter (2014) 1080p Action
Larry Crowne (2011) 1080p Comedy
Terminal Island (1973) 1080p Action
Heart of Midnight (1988) 1080p Drama
The Lift (1983) 1080p Fantasy
只需使用运算符检查字符串是否包含子字符串:

genre = ' '.join([item.next_sibling for item in title.select(".mdif li b") if "Genre:" in item.text])

你可以在item.text中使用
如果“Genre:”以及在item.text中使用
如果“nre:”如果在item.text中使用
如果在item.text中使用“Gen”,等等。

我不明白你的问题;但是,我要指出的是,Scrapy支持伪元素,等等:请看。有时我上面使用的文本足够长,可以像这样使用,这就是为什么我想使用部分匹配,而不是使用精确的文本。我也用过scrapy和lxml。然而,我想知道如何在BeautifulSoup中使用它。谢谢