Python:将一个数组中的字符串与另一个数组中的文本中的子字符串进行匹配
Python: Matching Strings from an Array with Substrings from Texts in another Array
目前我正在使用 Pythons BeautifulSoup 库抓取报纸文章的网页。这些文章存储在对象 "details".
中
然后我有几个不同街道的名称存储在对象 "lines" 中。现在我想在文章中搜索 "lines" 中包含的街道名称。
如果其中一个街道名称是其中一篇文章的一部分,我想将街道名称保存在一个数组中。
如果没有匹配的文章(所选文章不包含任何街道名称),则数组中应该有一个空元素。
例如,假设对象 "lines" 将包含 ("Abbey Road"、"St-John's Bridge"、"West Lane"、"Sunpoint"、"East End").
对象 "details" 由 4 篇文章组成,其中 2 篇包含 "Abbey Road" 和 "West Lane"(例如 "Car accident on Abbey Road, three people hurt")。其他 2 篇文章不包含 "lines".
中的任何名称
那么匹配后的结果应该是这样一个数组:
[]["Abbey Road"][]["West Lane"]
我还被告知为此使用矢量化,因为我的原始数据样本非常大。但是我不熟悉对 String 操作使用矢量化。有人用过这个吗?
我的代码目前看起来像这样,但是这只是 returns“-1”作为我的结果数组的元素:
from bs4 import BeautifulSoup
import requests
import io
import re
import string
import numpy as np
my_list = []
for y in range (0, 2):
y *= 27
i = str(y)
my_list.append('http://www.presseportal.de/blaulicht/suche.htx?q=' + 'einbruch' + '&start=' + i)
for link in my_list:
# print (link)
r = requests.get(link)
r.encoding = 'utf-8'
soup = BeautifulSoup(r.content, 'html.parser')
with open('a4.txt', encoding='utf8') as f:
lines = f.readlines()
lines = [w.replace('\n', '') for w in lines]
details = soup.find_all(class_='news-bodycopy')
for class_element in details:
details = class_element.get_text()
sdetails = ''.join(details)
slines = ''.join(lines)
i = str.find(sdetails, slines[1 : 38506])
print(i)
如果有人想重现我的实验,Website-Url在上面的代码中,对象"details"中的文章的抓取和存储工作正常,所以代码可以已复制。
我的对象 "lines" 原始数据的 .txt 文件可以在此 Dropbox 文件夹中访问:
https://www.dropbox.com/s/o0cjk1o2ej8nogq/a4.txt?dl=0
非常感谢您给我如何完成这项工作的任何提示,最好是通过矢量化。
您可以尝试这样的操作:
my_list = []
for y in range (0, 2):
i = str(y)
my_list.append('http://www.presseportal.de/blaulicht/suche.htx?q=einbruch&start=' + i)
for link in my_list:
r = requests.get(link)
soup = BeautifulSoup(r.content.decode('utf-8','ignore'), 'html.parser')
details = soup.find_all(class_='news-bodycopy')
f = open('a4.txt')
lines = [line.rstrip('\r\n') for line in f]
result = []
for i in range(len(details)):
found_in_line = 0
for j in range(len(lines)):
try:
if details[i].get_text().index(lines[j].decode('utf-8','ignore')) is not None:
result.append(lines[j])
found_in_line = found_in_line + 1
except:
if (j == len(lines)-1) and (found_in_line == 0):
result.append(" ")
print result
目前我正在使用 Pythons BeautifulSoup 库抓取报纸文章的网页。这些文章存储在对象 "details".
中然后我有几个不同街道的名称存储在对象 "lines" 中。现在我想在文章中搜索 "lines" 中包含的街道名称。
如果其中一个街道名称是其中一篇文章的一部分,我想将街道名称保存在一个数组中。
如果没有匹配的文章(所选文章不包含任何街道名称),则数组中应该有一个空元素。
例如,假设对象 "lines" 将包含 ("Abbey Road"、"St-John's Bridge"、"West Lane"、"Sunpoint"、"East End").
对象 "details" 由 4 篇文章组成,其中 2 篇包含 "Abbey Road" 和 "West Lane"(例如 "Car accident on Abbey Road, three people hurt")。其他 2 篇文章不包含 "lines".
中的任何名称那么匹配后的结果应该是这样一个数组: []["Abbey Road"][]["West Lane"]
我还被告知为此使用矢量化,因为我的原始数据样本非常大。但是我不熟悉对 String 操作使用矢量化。有人用过这个吗?
我的代码目前看起来像这样,但是这只是 returns“-1”作为我的结果数组的元素:
from bs4 import BeautifulSoup
import requests
import io
import re
import string
import numpy as np
my_list = []
for y in range (0, 2):
y *= 27
i = str(y)
my_list.append('http://www.presseportal.de/blaulicht/suche.htx?q=' + 'einbruch' + '&start=' + i)
for link in my_list:
# print (link)
r = requests.get(link)
r.encoding = 'utf-8'
soup = BeautifulSoup(r.content, 'html.parser')
with open('a4.txt', encoding='utf8') as f:
lines = f.readlines()
lines = [w.replace('\n', '') for w in lines]
details = soup.find_all(class_='news-bodycopy')
for class_element in details:
details = class_element.get_text()
sdetails = ''.join(details)
slines = ''.join(lines)
i = str.find(sdetails, slines[1 : 38506])
print(i)
如果有人想重现我的实验,Website-Url在上面的代码中,对象"details"中的文章的抓取和存储工作正常,所以代码可以已复制。
我的对象 "lines" 原始数据的 .txt 文件可以在此 Dropbox 文件夹中访问: https://www.dropbox.com/s/o0cjk1o2ej8nogq/a4.txt?dl=0
非常感谢您给我如何完成这项工作的任何提示,最好是通过矢量化。
您可以尝试这样的操作:
my_list = []
for y in range (0, 2):
i = str(y)
my_list.append('http://www.presseportal.de/blaulicht/suche.htx?q=einbruch&start=' + i)
for link in my_list:
r = requests.get(link)
soup = BeautifulSoup(r.content.decode('utf-8','ignore'), 'html.parser')
details = soup.find_all(class_='news-bodycopy')
f = open('a4.txt')
lines = [line.rstrip('\r\n') for line in f]
result = []
for i in range(len(details)):
found_in_line = 0
for j in range(len(lines)):
try:
if details[i].get_text().index(lines[j].decode('utf-8','ignore')) is not None:
result.append(lines[j])
found_in_line = found_in_line + 1
except:
if (j == len(lines)-1) and (found_in_line == 0):
result.append(" ")
print result