小编Dan*_*Eng的帖子

正则表达式用匹配词提取整个句子

我想在整篇文章中用"flung"这个词来提取句子.

例如,在下面的文字中,我想提取一句话"就好像一只手把它们抓在中间并把它们扔到一边." 使用正则表达式.

我尝试使用它.*? flung (?<sub>.*?)\.,但它从行的开头开始搜索.

我怎么能解决这个问题?

当她这样做时,发生了一件非同寻常的事情.床上衣服聚集在一起,突然跳起来,形成一种高峰,然后一头扎进底部铁轨.就好像一只手将它们紧紧抓住中心并将它们扔到一边.之后立马, .........

regex

9
推荐指数
2
解决办法
2万
查看次数

RegEx使用匹配单词提取句子,而不停在"Mr.","Mrs." 等等

我创建了一个正则表达式,可以提取包含匹配单词的句子.

[^.|?|!]*\<friends\>[^.|!|?]*[\"!?:\.]
Run Code Online (Sandbox Code Playgroud)

但是,它不适用于句子中有Mr.Mrs./ Dr.等的情况.

例如:

The adventures are great. I don't know whether you know that Dr. Watson and Mr. Holmes are good friends, Ms. Adler.
Run Code Online (Sandbox Code Playgroud)

我想要的输出是:

I don't know whether you know that Dr. Watson and Mr. Holmes are good friends, Ms. Adler.
Run Code Online (Sandbox Code Playgroud)

这该怎么做?

regex

5
推荐指数
1
解决办法
508
查看次数

Python 中的多处理:网页抓取没有加速

我想使用多处理模块来加速网页抓取。我的目标是在页面中提取一部分 HTML 并将其保存在父变量中。最后,将该变量写入文件。

但我遇到的问题是处理页面大约需要 1 秒。

我的代码有效,但它没有做我想要的:

import urllib.request
from bs4 import BeautifulSoup
from multiprocessing.dummy import Pool  # This is a thread-based Pool
from multiprocessing import cpu_count


def parseWeb(url):
    page = urllib.request.urlopen(url)
    soup = BeautifulSoup(page)
    h2_tag = soup.find('h2', class_='midashigo')
    return h2_tag

if __name__ == '__main__':
    file = 'links.txt' # each link is on a separate line.
    pool = Pool(cpu_count() * 2)
    with open(file, 'r') as f:
        results = pool.map(parseWeb, f)
    with open('output.txt', 'w', encoding='utf-8') as w:
        w.write(str(results))
Run Code Online (Sandbox Code Playgroud)

如何修改它以赋予它多处理的全部功能?谢谢你。

python multiprocessing web-scraping

1
推荐指数
1
解决办法
3476
查看次数

Python Dictionary调用Dictionary但TypeError:string索引必须是整数

我想从调用字典的结果中调用另一个字典.但是,有一个错误.

>>> dict1 = {1:'apple', 2:'mango', 3:'pear'}
>>> dict2 = {'apple': 'jason', 'mango': 'mary', 'pear':'susan'}
>>> print(dict1[1]['apple'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string indices must be integers
Run Code Online (Sandbox Code Playgroud)

所需的输出是: jason

python dictionary

-1
推荐指数
1
解决办法
222
查看次数