python - Python 请求 xpath web
问题描述
我仍然是初学者,我正在尝试使用请求从配置文件链接中提取位置,这是我的代码,并且我已经解码了 page.content(你会在输出中找到它)。我没有在 html 代码中找到我的 xpath
from lxml import html
import requests
page = requests.get('https://www.linkedin.com/in/peter-zorich-9567a610/')
tree = html.fromstring(page.content)
location = tree.xpath('/html/body/div[8]/div[3]/div/div/div/div/div[2]/main/div[1]/section/div[2]/div[2]/div[1]/ul[2]/li[1]')
print(location)
x = page.content.decode("utf-8")
print(x)
file = open(r'C:\Users\Hamza\Desktop\file_text.txt', 'w')
file.write(x)
输出: []
<html><head>
<script type="text/javascript">
window.onload = function() {
// Parse the tracking code from cookies.
var trk = "bf";
var trkInfo = "bf";
var cookies = document.cookie.split("; ");
for (var i = 0; i < cookies.length; ++i) {
if ((cookies[i].indexOf("trkCode=") == 0) && (cookies[i].length > 8)) {
trk = cookies[i].substring(8);
}
else if ((cookies[i].indexOf("trkInfo=") == 0) && (cookies[i].length > 8)) {
trkInfo = cookies[i].substring(8);
}
}
if (window.location.protocol == "http:") {
// If "sl" cookie is set, redirect to https.
for (var i = 0; i < cookies.length; ++i) {
if ((cookies[i].indexOf("sl=") == 0) && (cookies[i].length > 3)) {
window.location.href = "https:" + window.location.href.substring(window.location.protocol.length);
return;
}
}
}
// Get the new domain. For international domains such as
// fr.linkedin.com, we convert it to www.linkedin.com
var domain = "www.linkedin.com";
if (domain != location.host) {
var subdomainIndex = location.host.indexOf(".linkedin");
if (subdomainIndex != -1) {
domain = "www" + location.host.substring(subdomainIndex);
}
}
window.location.href = "https://" + domain + "/authwall?trk=" + trk + "&trkInfo=" + trkInfo +
"&originalReferer=" + document.referrer.substr(0, 200) +
"&sessionRedirect=" + encodeURIComponent(window.location.href);
}
</script>
</head></html>
解决方案
推荐阅读
- ruby - 如何在 ruby 脚本中使用 awscli?
- c# - 调用 REST API,我在控制台内收到“未收到数据”消息,但能够进行身份验证
- asp.net-mvc - @Html.DisplayNameFor 子字符串?
- reactjs - 用于动态设置 css-classes 的自定义 react-hook
- powershell - 如何将 Device\HarddiskVolume[n] 与驱动器号相关联
- python - Python 中如何定义分类变量的级别?
- swift - 为什么这在一个地方有效,而在另一个地方无效?
- python - 尝试在 Azure 中使用 matplotlib 时出现“ModuleNotFoundError: No module named 'tkinter'”
- 7zip - 如何解压缩到包含名称中的点的文件夹中
- c# - WPF:数据绑定跟踪的性能影响