headers = table.xpath("//thead/tr/th") ^^^^^^^^^^^ AttributeError: 'list' object has no attribute 'xpath'
时间: 2023-02-21 07:35:36 浏览: 71
这个错误的意思是说,变量"table"是一个列表,但是列表对象没有"xpath"属性。
这可能是因为你在使用xpath语法时,将xpath应用到了错误的对象上。xpath是用于选取XML和HTML文档中的元素的语言,通常是使用lxml库或者BeautifulSoup库实现的。如果你想使用xpath,请确保"table"变量是一个lxml.html或者BeautifulSoup对象,而不是一个列表。
相关问题
ef get_video_url("https://v.douyin.com/UsnmrMh/"): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: invalid syntax
这是因为你在函数定义时没有为函数指定名称。函数定义应该是 `def 函数名(参数列表):` 的形式。你可以将函数名改为 `get_video_url`,代码应该像这样:
```python
import requests
import re
def get_video_url(url):
# 从分享链接中提取视频id
video_id = re.findall(r'video/(\d+)', url)[0]
# 构造API请求URL
api_url = 'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids={}'.format(video_id)
# 添加请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
# 发送API请求
response = requests.get(api_url, headers=headers)
# 解析API响应,获取无水印视频下载链接
video_info = response.json()['item_list'][0]
video_url = video_info['video']['play_addr']['url_list'][0].replace('playwm', 'play')
return video_url
# 测试
url = 'https://v.douyin.com/J9D6BQx/'
video_url = get_video_url(url)
print(video_url)
```
运行时将分享链接作为 `url` 变量的值传递给 `get_video_url` 函数即可。
Traceback (most recent call last): File "D:\JetBrains\PyCharm\plugins\python\helpers\pydev\pydevconsole.py", line 364, in runcode coro = func() ^^^^^^ File "<input>", line 1, in <module> File "D:\JetBrains\PyCharm\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\JetBrains\PyCharm\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "D:\Projects\pythonProject\停车系统\temp.py", line 45, in <module> close_proxy() File "D:\Projects\pythonProject\停车系统\temp.py", line 39, in close_proxy mitmdump_process.stop() ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'Popen' object has no attribute 'stop'
抱歉,我的回答有误。Popen对象没有stop()方法,您应该使用terminate()方法来停止进程。具体代码示例如下:
```
from subprocess import Popen
class AddHeader:
def request(self, flow: http.HTTPFlow) -> None:
self.num += 1
if "https://cloud.keytop.cn/service/front/userCenter/lpn/list?_=" in flow.request.pretty_url:
for name, value in flow.request.headers.items():
if "accesstoken:" == name:
accesstoken = value
addons = [
AddHeader()
]
def set_proxy():
"""设置系统代理"""
global mitmdump_process
pxy.enable = True
pxy.server = '127.0.0.1:8081'
pxy.registry_write()
mitmdump_process = Popen(['mitmdump', '-p', '8081', '-s', __file__])
def close_proxy():
"""关闭系统代理"""
pxy.enable = False
pxy.registry_write()
mitmdump_process.terminate()
if __name__ == "__main__":
pxy = ProxySetting()
set_proxy()
close_proxy()
```
在这个示例中,我们使用了subprocess模块中的Popen()函数来创建mitmdump进程对象,并将其赋值给全局变量mitmdump_process。在close_proxy()中,我们只需要调用mitmdump进程对象的terminate()方法即可停止mitmdump进程。
阅读全文