Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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_Web Scraping_Beautifulsoup_Persian - Fatal编程技术网

Python 美丽的汤发现使用波斯字符串

Python 美丽的汤发现使用波斯字符串,python,web-scraping,beautifulsoup,persian,Python,Web Scraping,Beautifulsoup,Persian,我想在Python中使用Beauty Soup查找包含字符串的所有元素 它在我使用非波斯语字符时有效,但在我使用波斯语字符时无效 from bs4 import BeautifulSoup QUERY = 'رشته فارسی' URL = 'http://www.example.com' headers = { 'User-Agent': "Mozilla/5.0 . . . " } request = urllib2.Request(URL, headers=headers) resp

我想在Python中使用Beauty Soup查找包含字符串的所有元素

它在我使用非波斯语字符时有效,但在我使用波斯语字符时无效

from bs4 import BeautifulSoup
QUERY = 'رشته فارسی'
URL = 'http://www.example.com'
headers = {
  'User-Agent': "Mozilla/5.0 . . . "
}
request = urllib2.Request(URL, headers=headers)
response = urllib2.urlopen(request)
response_content = response.read().decode('utf8')
soup = BeautifulSoup(response_content, 'html.parser')
fetched = soup.find_all(text=QUERY)
print(fetched)
对于上面的代码,输出是
[]
,但是如果在查询中使用ASCII,它就可以工作

是否有任何UTF-8转换或其他解决方法:)?

#-*-编码:UTF-8-*-
导入urllib2
从bs4导入BeautifulSoup
查询='
URL='1〕https://bayan.ir/service/bayan/'
标题={
“用户代理”:“Mozilla/5.0…”
}
request=urlib2.request(URL,headers=headers)
response=urllib2.urlopen(请求)
response\u content=response.read()
soup=BeautifulSoup(响应内容'html.parser')
fetched=soup.find_all(string=QUERY)
打印(已提取)

它起作用了

您需要匹配上的编码page@PadraicCunningham我怎么做呢?你不应该用utf-8或utf-8来代替utf8吗?你在用Python 3,对吗?我不懂波斯语,但您是否尝试过
查询
响应内容
?因为两个字符串看起来相同,所以它们不必由相同的代码点组成(对于计算机,它们不必看起来相同)。当您
打印(repr(QUERY))
看到什么?您必须发送精确的字符串检查