python错误提示pattern=re.compile('<div><p>.*</p></div>',re.S) AttributeError: module 're' has no attribute 'compile
时间: 2023-11-02 22:38:05 浏览: 52
这个错误提示意味着在你的代码中,Python 解释器无法找到 `re.compile` 方法。这通常是由于导入 `re` 模块失败或者 `re` 模块中确实没有 `compile` 方法导致的。
可以尝试以下两种方法:
1. 检查是否正确导入了 `re` 模块。在代码的开头添加 `import re` 语句确保已经导入该模块。
2. 检查 Python 版本是否正确。在 Python 2 中,`re.compile` 是作为方法使用的,而在 Python 3 中,`re.compile` 是作为函数使用的。如果你的 Python 版本是 2.x,则应该使用 `re.compile()` 方法。如果你的 Python 版本是 3.x,则应该使用 `re.compile` 函数。
相关问题
<td>2023058</td> <td>10 17 22 26 30 33 + 11</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td >0</td> </tr> <tr class="new"> <td>2023059</td> <td></td> <td>24</td> <td>10</td> <td>26</td> <td>31</td> <td>12</td> <td>33</td> <td>24</td> <td>08</td> <td>31</td> <td>24</td> <td></td> </tr> <tr> <td colspan="2">正确次数</td> <td>4</td> <td>3</td> <td>7</td> <td>2</td> <td>9</td> <td>3</td> <td>4</td> <td>5</td> <td>3</td> <td>6</td> <td></td> </tr> <tr> <td colspan="2">错误次数</td> <td>16</td> <td>17</td> <td>13</td> <td>18</td> <td>11</td> <td>17</td> <td>16</td> <td>15</td> <td>17</td> <td>14</td> <td></td> </tr> <tr> <td colspan="2">当前连对</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td></td> </tr> <tr> <td colspan="2">当前连错</td> <td>1</td> <td>4</td> <td>2</td> <td>4</td> <td>1</td> <td>7</td> <td>1</td> <td>2</td> <td>13</td> <td>3</td> <td></td> </tr> <tr> <td colspan="2">最大连对</td> <td>2</td> <td>1</td> <td>3</td> <td>1</td> <td>2</td> <td>1</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td></td> </tr> <tr> <td colspan="2">最大连错</td> <td>9</td> <td>7</td> <td>4</td> <td>12</td> <td>3</td> <td>7</td> <td>7</td> <td>4</td> <td>13</td> <td>3</td> <td></td> </tr> </table> </div> </div> </div> <script type="text/javascript" src="https://img.78500.cn/mobile/iscroll.js"></script> <script type="text/javascript"> var wrapper; function loaded() { wrapper = new iScroll("wrapper", { vScrollbar: false, hScrollbar: false, zoom: true, zoomMin: 0.5, zoomMax: 1 }); } document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false); document.addEventListener('DOMContentLoaded', loaded, false); </script></body> </html> 对应上代码用正则表达式提取 以 <tr class="new"> 开头 </tr>结束 里面的数字
可以使用以下正则表达式进行提取:
```python
import re
text = """<table class="table table-bordered table-condensed"> <thead> <tr> <th>期号</th> <th colspan="11">开奖号码</th> <th>总和</th> </tr> </thead> <tbody> <tr class="new"> <td>2023058</td> <td>10 17 22 26 30 33 + 11</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>0</td> </tr> <tr class="new"> <td>2023059</td> <td></td> <td>24</td> <td>10</td> <td>26</td> <td>31</td> <td>12</td> <td>33</td> <td>24</td> <td>08</td> <td>31</td> <td>24</td> <td></td> </tr> <tr> <td colspan="2">正确次数</td> <td>4</td> <td>3</td> <td>7</td> <td>2</td> <td>9</td> <td>3</td> <td>4</td> <td>5</td> <td>3</td> <td>6</td> <td></td> </tr> <tr> <td colspan="2">错误次数</td> <td>16</td> <td>17</td> <td>13</td> <td>18</td> <td>11</td> <td>17</td> <td>16</td> <td>15</td> <td>17</td> <td>14</td> <td></td> </tr> <tr> <td colspan="2">当前连对</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td></td> </tr> <tr> <td colspan="2">当前连错</td> <td>1</td> <td>4</td> <td>2</td> <td>4</td> <td>1</td> <td>7</td> <td>1</td> <td>2</td> <td>13</td> <td>3</td> <td></td> </tr> <tr> <td colspan="2">最大连对</td> <td>2</td> <td>1</td> <td>3</td> <td>1</td> <td>2</td> <td>1</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td></td> </tr> <tr> <td colspan="2">最大连错</td> <td>9</td> <td>7</td> <td>4</td> <td>12</td> <td>3</td> <td>7</td> <td>7</td> <td>4</td> <td>13</td> <td>3</td> <td></td> </tr> </table> </div> </div> </div> <script type="text/javascript" src="https://img.78500.cn/mobile/iscroll.js"></script> <script type="text/javascript"> var wrapper; function loaded() { wrapper = new iScroll("wrapper", { vScrollbar: false, hScrollbar: false, zoom: true, zoomMin: 0.5, zoomMax: 1 }); } document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false); document.addEventListener('DOMContentLoaded', loaded, false); </script></body> </html>"""
pattern = re.compile(r'<tr class="new">(.*?)</tr>', re.S)
matches = pattern.findall(text)
for match in matches:
print(match)
```
这将输出以下内容:
```
<td>2023058</td> <td>10 17 22 26 30 33 + 11</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>×</td> <td>0</td>
<td>2023059</td> <td></td> <td>24</td> <td>10</td> <td>26</td> <td>31</td> <td>12</td> <td>33</td> <td>24</td> <td>08</td> <td>31</td> <td>24</td> <td></td>
```
<div class="totalPrice totalPrice2"><i>参考价: </i><span class="">395</span><i>万</i></div>如何只取395
您可以使用字符串切片或正则表达式来提取数字部分。以下是字符串切片的示例代码:
```python
price_str = '<div class="totalPrice totalPrice2"><i>参考价: </i><span class="">395</span><i>万</i></div>'
start_index = price_str.index('">') + 2
end_index = price_str.index('</span>')
price = price_str[start_index:end_index]
print(price)
```
输出结果为:
```
395
```
以下是正则表达式的示例代码:
```python
import re
price_str = '<div class="totalPrice totalPrice2"><i>参考价: </i><span class="">395</span><i>万</i></div>'
pattern = re.compile(r'<span.*?>(.*?)</span>')
match = pattern.search(price_str)
if match:
price = match.group(1)
print(price)
```
输出结果为:
```
395
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
import requests import re # from bs4 import BeautifulSoup import matplotlib.pyplot as plt import numpy as np # import pandas as pd i = 1 lists = [0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250] title = [] year = [] country = [] score = [] number = [] for page in range(0, 226, 25): url = 'https://movie.douban.com/top250?start=' + str(page) + '&filter=' headers = { 'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"} resp = requests.get(url=url, headers=headers) resp.encoding = "utf-8" pattern = re.compile( r'.*? < img width="100" alt="(?P<title>.*?)".*?class="">.*?.*?导演: (?P<director>.*?) .*?
.*?(?P<year>.*?) / (?P<country>.*?) .*?"v:average">(?P<score>.*?).*?(?P<number>.*?)人评价', re.S) pic_url = re.compile(r'< img width="100".*?src="(.*?)" class="">', re.S) pic_URl = pic_url.findall(resp.text) data2 = pattern.finditer(str(resp.text)) for url1 in pic_URl: file1 = open('films.pic\\' + str(i) + '.jpg', 'ab') Pic = requests.get(url1) file1.write(Pic.content) i = i + 1 file1.close() file2 = open('movie.text', 'a+', encoding='utf-8') for m in data2: if int(m['number']) / 100000 > 13: number.append(int(m['number']) / 100000) country.append(m['country']) year.append(m['year']) title.append(m['title']) score.append(m['score']) file2.write( '电影名:' + m['title'] + ', 导演:' + m['director'] + ', 年份:' + m['year'] + ', 国家:' + m['country'] + ', 评分:' + m[ 'score'] + ',评价人数:' + str(int(m['number']) / 100000) + ' 100k') file2.write('\n') print( '电影名:' + m['title'] + ', 导演:' + m['director'] + ', 年份:' + m['year'] + ', 国家:' + m['country'] + ', 评分:' + m[ 'score'] + ',评价人数:' + str(int(m['number']) / 100000) + ' 100k')
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)