WatermelonPy 2022-04-06 23:40 采纳率: 77.8%
浏览 159
已结题

Python 的re为什么匹配不到?正则表达式没错呀

import requests
import re


domain = "https://dy.dytt8.net/index2.htm"
resp = requests.get(domain, verify=False)
resp.encoding = "gb2312"

obj1 = re.compile(r"最新影片推荐.*?<ul>(?P<cont>.*?)</ul>", re.S)
obj2 = re.compile(r"<a href='(?P<href>.*?)'", re.S)
obj3 = re.compile(r'◎片  名(?P<name>.*?)<br />.*?'
                  r'<a target="_blank" href="(?P<download>.*?)">', re.S)

result1 = obj1.finditer(resp.text)
child_href_list = []

for it in result1:
    ul = it.group('ul')

    # 提取子页面链接:
    result2 = obj2.finditer(ul)
    for itt in result2:
        # 拼接子页面的url地址:  域名 + 子页面地址
        child_href = domain + itt.group('href')
        child_href1 = child_href.replace("/index2.htm", "")
        child_href_list.append(child_href1) # 把子页面链接保存起来

for href in child_href_list:
    child_resp = requests.get(href, verify=False)
    child_resp.encoding = 'gb2312'
    result3 = obj3.search(child_resp.text)
    print(result3.group("movie"))
    print(result3.group("download"))

输出为

C:\Users\pc\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/pc/AppData/Roaming/JetBrains/PyCharmCE2021.3/scratches/scratch_13.py
C:\Users\pc\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'dy.dytt8.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
C:\Users\pc\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'dy.dytt8.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
 �C���槭骏�ンダム  �W光のハサウェイ
magnet:?xt=urn:btih:d022c54fb3bd54a9526d5f137fb5c03c2f4c5cae&amp;dn=%e9%98%b3%e5%85%89%e7%94%b5%e5%bd%b1www.ygdy8.com.%e6%9c%ba%e5%8a%a8%e6%88%98%e5%a3%ab%e9%ab%98%e8%be%be%e9%97%aa%e5%85%89%e7%9a%84%e5%93%88%e8%90%a8%e7%bb%b4.2021.BD.1080P.%e6%97%a5%e8%af%ad%e4%b8%ad%e5%ad%97.mkv&amp;tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&amp;tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce
C:\Users\pc\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'dy.dytt8.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
 The  Alpinist
magnet:?xt=urn:btih:13d1e2e985f47583670461b76f2133fd7dcf1107&amp;dn=%e9%98%b3%e5%85%89%e7%94%b5%e5%bd%b1www.ygdy8.com.%e7%99%bb%e5%b1%b1%e5%ae%b6.2021.BD.1080P.%e4%b8%ad%e8%8b%b1%e5%8f%8c%e5%ad%97.mkv&amp;tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&amp;tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce
C:\Users\pc\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'dy.dytt8.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
 Spider-Man:  No Way Home/Spider-Man 3/Spider-Man: Home Slice/Spider-Man:  Home-Wrecker/Spider-Man: Phone  Home
magnet:?xt=urn:btih:9c6468e3eb844ff602331fa914b0a6993c184874&amp;dn=%e9%98%b3%e5%85%89%e7%94%b5%e5%bd%b1www.ygdy8.com.%e8%9c%98%e8%9b%9b%e4%be%a0%ef%bc%9a%e8%8b%b1%e9%9b%84%e6%97%a0%e5%bd%92.2021.BD.1080P.%e4%b8%ad%e8%8b%b1%e5%8f%8c%e5%ad%97.mkv&amp;tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&amp;tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce
C:\Users\pc\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'dy.dytt8.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
 Turning  Red
magnet:?xt=urn:btih:4bf685d1d0fa984df5b49f48c4816c07ae4d0457&amp;dn=%e9%98%b3%e5%85%89%e7%94%b5%e5%bd%b1www.ygdy8.com.%e9%9d%92%e6%98%a5%e5%8f%98%e5%bd%a2%e8%ae%b0.2022.BD.1080P.%e4%b8%ad%e8%8b%b1%e5%8f%8c%e5%ad%97.mkv&amp;tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&amp;tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce
C:\Users\pc\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'dy.dytt8.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
 Petite  maman / Lille mamma
magnet:?xt=urn:btih:153331d979eb402d46960be3944d02f93f170c20&amp;dn=%e9%98%b3%e5%85%89%e7%94%b5%e5%bd%b1www.ygdy8.com.%e5%b0%8f%e5%a6%88%e5%a6%88.2021.BD.1080P.%e4%b8%ad%e8%8b%b1%e5%8f%8c%e5%ad%97.mkv&amp;tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&amp;tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce
C:\Users\pc\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'dy.dytt8.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
 West  Side Story
magnet:?xt=urn:btih:b0ab691d2be3126e77d93e72e0ee495ea0378740&amp;dn=%e9%98%b3%e5%85%89%e7%94%b5%e5%bd%b1www.ygdy8.com.%e8%a5%bf%e5%8c%ba%e6%95%85%e4%ba%8b.2021.BD.1080P.%e4%b8%ad%e8%8b%b1%e5%8f%8c%e5%ad%97.mkv&amp;tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&amp;tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce
C:\Users\pc\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'dy.dytt8.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
 Madres  paralelas / Parallel  Mothers
magnet:?xt=urn:btih:b4bf3f0114059ff049da5958bedc55a65934530d&amp;dn=%e9%98%b3%e5%85%89%e7%94%b5%e5%bd%b1www.ygdy8.com.%e5%b9%b3%e8%a1%8c%e6%af%8d%e4%ba%b2.2021.BD.1080P.%e8%a5%bf%e7%8f%ad%e7%89%99%e8%af%ad%e4%b8%ad%e5%ad%97.mkv&amp;tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&amp;tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce
C:\Users\pc\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'dy.dytt8.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
 Found
magnet:?xt=urn:btih:e2f20c76bfa11d46f81a6d6b04b283c263aecca0&amp;dn=%e9%98%b3%e5%85%89%e7%94%b5%e5%bd%b1www.ygdy8.com.%e6%89%be%e5%af%bb.2021.BD.1080P.%e4%b8%ad%e8%8b%b1%e5%8f%8c%e5%ad%97.mkv&amp;tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&amp;tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce
C:\Users\pc\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'dy.dytt8.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
 Le  Sommet des Dieux / The Summit of the  Gods
magnet:?xt=urn:btih:fa06021716929e4c5de250835e7328f747b3fe67&amp;dn=%e9%98%b3%e5%85%89%e7%94%b5%e5%bd%b1www.ygdy8.com.%e7%a5%9e%e4%b9%8b%e5%b1%b1%e5%b2%ad.2021.BD.1080P.%e6%b3%95%e8%af%ad%e4%b8%ad%e5%ad%97.mkv&amp;tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&amp;tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce
C:\Users\pc\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'dy.dytt8.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
 Scream/Scream  5
magnet:?xt=urn:btih:3568c07aa97af473fd547eec7649f94239150939&amp;dn=%e9%98%b3%e5%85%89%e7%94%b5%e5%bd%b1www.ygdy8.com.%e6%83%8a%e5%a3%b0%e5%b0%96%e5%8f%ab5.2022.BD.1080P.%e4%b8%ad%e8%8b%b1%e5%8f%8c%e5%ad%97.mkv&amp;tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&amp;tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce
C:\Users\pc\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'dy.dytt8.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
 映画クレヨンしんちゃん  �iメキ!花の天カス学�@
magnet:?xt=urn:btih:acd85204ef63f4c09bdf42f583566af763e9c9c8&amp;dn=%e9%98%b3%e5%85%89%e7%94%b5%e5%bd%b1www.ygdy8.com.%e8%9c%a1%e7%ac%94%e5%b0%8f%e6%96%b0%ef%bc%9a%e8%b0%9c%e5%9b%a2.2021.BD.1080P.%e5%9b%bd%e7%b2%a4%e6%97%a5%e4%b8%89%e8%af%ad%e4%b8%ad%e5%ad%97.mkv&amp;tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&amp;tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce
C:\Users\pc\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'dy.dytt8.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
 King  Richard
magnet:?xt=urn:btih:72641444ec15aabadf1aa32d28a601e66ec66b27&amp;dn=%e9%98%b3%e5%85%89%e7%94%b5%e5%bd%b1www.ygdy8.com.%e5%9b%bd%e7%8e%8b%e7%90%86%e6%9f%a5%e5%be%b7.2021.BD.1080P.%e4%b8%ad%e8%8b%b1%e5%8f%8c%e5%ad%97.mkv&amp;tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&amp;tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce
C:\Users\pc\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'dy.dytt8.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
 American  Underdog: The Kurt Warner Story/American  Underdog
magnet:?xt=urn:btih:db673fd0dd7ecad002183657112732e38727df77&amp;dn=%e9%98%b3%e5%85%89%e7%94%b5%e5%bd%b1www.ygdy8.com.%e7%be%8e%e5%9b%bd%e8%8d%89%e6%a0%b9%ef%bc%9a%e5%ba%93%e5%b0%94%e7%89%b9%c2%b7%e5%8d%8e%e7%ba%b3%e7%9a%84%e6%95%85%e4%ba%8b.2021.BD.1080P.%e4%b8%ad%e8%8b%b1%e5%8f%8c%e5%ad%97.mkv&amp;tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&amp;tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce
C:\Users\pc\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'dy.dytt8.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
Traceback (most recent call last):
  File "C:\Users\pc\AppData\Roaming\JetBrains\PyCharmCE2021.3\scratches\scratch_13.py", line 31, in <module>
    print(result3.group("movie"))
AttributeError: 'NoneType' object has no attribute 'group'

Process finished with exit code 1

匹配的内容为

img

  • 写回答

1条回答 默认 最新

  • weixin_月读 2022-04-07 14:21
    关注

    你打印下result1,得出来的不是个列表啊,finditer匹配还没用过,

    img


    看这个得到的是个对象,不能直接循环它,可以考虑用用findall

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月16日
  • 已采纳回答 4月8日
  • 修改了问题 4月6日
  • 创建了问题 4月6日

悬赏问题

  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路
  • ¥15 内网办公电脑进行向日葵
  • ¥15 如何输入双曲线的参数a然后画出双曲线?我输入处理函数加上后就没有用了,不知道怎么回事去掉后双曲线可以画出来
  • ¥15 soildworks装配体的尺寸问题
  • ¥100 有偿寻云闪付SDK转URL技术